nested-attributes

Can't mass-assign protected attributes

落花浮王杯 提交于 2019-12-20 16:22:48
问题 Updating the code formatting for better viewing. Folks, I have been looking at this for sometime but I don't understand what could be messing up here. I am using Devise. class User < ActiveRecord::Base has_many :addresses accepts_nested_attributes_for :addresses # Other stuff here end class Address < ActiveRecord::Base belongs_to :user validates_presence_of :zip #:street_address1, end -------------------- log output begin ------------------------------ Started POST "/users" for 127.0.0.1 at

strong parameter and json input rails 4

北城以北 提交于 2019-12-19 02:47:40
问题 I am trying to save data through JSON String in which I have nested associated attributes. I do not want to use attr_accessible. I almost got the logic of strong parameter but still got the problem to make them work. I am getting JSON string and using it to save data using this data = request.body.read @inputData = Person.new(JSON.parse(data)) @inputData.save! if@inputData.valid? render :status => 200, :json => "Data inserted successfully" else render :status => 404, :json => "Not Inserted "

Ruby on Rails - nested attributes: How do I access the parent model from child model

岁酱吖の 提交于 2019-12-17 22:37:05
问题 I have a couple of models like so class Bill < ActiveRecord::Base has_many :bill_items belongs_to :store accepts_nested_attributes_for :bill_items end class BillItem <ActiveRecord::Base belongs_to :product belongs_to :bill validate :has_enough_stock def has_enough_stock stock_available = Inventory.product_is(self.product).store_is(self.bill.store).one.quantity errors.add(:quantity, "only #{stock_available} is available") if stock_available < self.quantity end end The above validation so

Rails 3.1+ Nested Forms Issue: Can't mass-assign protected attributes

冷暖自知 提交于 2019-12-17 21:18:00
问题 I have a basketball app, where a Roster has many Players, and a Player can be on multiple Rosters.(Reason for many-to-many is for a Player-Roster archive) Roster.rb class Roster < ActiveRecord::Base belongs_to :team has_many :rosterizes has_many :players, :through => :rosterizes accepts_nested_attributes_for :players attr_accessible :jersey_number, :team_id, :class_year, :players end Rosterizes.rb (poorly named I know...) class Rosterize < ActiveRecord::Base belongs_to :player belongs_to

accepts_nested_attributes_for to link to existing record, not create a new one

跟風遠走 提交于 2019-12-17 18:28:38
问题 I have the following models class Order < AR::Base has_many :products accepts_nested_attributes_for :products end class Product < AR::Base belongs_to :order has_and_belongs_to_many :stores accepts_nested_attributes_for :stores end class Store < AR::Base has_and_belongs_to_many :products end Now I have a order view where I want to update the shops for the product. The thing is that I only want to connect the products to the existing shops in my db, not create new ones. My form in the order

Nested models and parent validation

拟墨画扇 提交于 2019-12-17 15:22:24
问题 I've got two models. - Parent has_many Children ; - Parent accepts_nested_attributes_for Children ; class Parent < ActiveRecord::Base has_many :children, :dependent => :destroy accepts_nested_attributes_for :children, :allow_destroy => true validates :children, :presence => true end class Child < ActiveRecord::Base belongs_to :parent end I use validation to validate presence of children for every parent, so I can't save parent without children. parent = Parent.new :name => "Jose" parent.save

Rails 4 - Strong Parameters - Nested Objects

时光毁灭记忆、已成空白 提交于 2019-12-17 01:44:08
问题 I've got a pretty simple question. But haven't found a solution so far. So here's the JSON string I send to the server: { "name" : "abc", "groundtruth" : { "type" : "Point", "coordinates" : [ 2.4, 6 ] } } Using the new permit method, I've got: params.require(:measurement).permit(:name, :groundtruth) This throws no errors, but the created database entry contains null instead of the groundtruth value. If I just set: params.require(:measurement).permit! Everything get's saved as expected, but of

undefined method `[]' for nil:NilClass Ruby Object in lib Rails

China☆狼群 提交于 2019-12-14 04:23:25
问题 I have an app with a fairly complicated form that uses Ruby Objects in the lib folder (my first time with ruby objects). I can't figure out how to create the multiple :task_ids in my Update Nested Attributes form, even though the params are defined in my Ruby Object Custom Class (this is my first time using this and I am learning how to call it). I keep getting this error: undefined method `[]' for nil:NilClass What my form is trying to do is create starter tasks & starter milestones based on

Rails polymorphic link for nested index action

社会主义新天地 提交于 2019-12-14 03:46:33
问题 I have been trying to find this one link for hours now. I have a polymorphic association where both collections & assortments have designs. Collection model has_many :designs, :as => :targetable Assortment model has_many :designs, :as => :targetable Design model belongs_to :targetable, :polymorphic => true In order to link to the design's 'show' action, the proper polymorphic path would be: link_to polymorphic_path([@targetable, @design]) But I can't figure out how to link to the design's

How to save form data to different models with one-to-one relationship in rails 3?

北慕城南 提交于 2019-12-13 20:22:56
问题 I have a user controller and a user model. In my user table I have limited fields. But now I want to create separate tables for user bank info and user personal info and save through the one form only. How is it possible, I am sure their must be something for this problem? 回答1: Check how nested_forms works along with the view helpers. Basically you add this in your user class : class User < AR has_one :profile has_one :address accepts_nested_attributes_for :profile, :adress attr_accessible