Ruby on Rails - Parent object to create with child object attached
I have a parent object that has a polymorphic-has_many association with a child. The child does not have any backward-relation to its parent. It only has a polymorphic relation with itself. Using AJAX I would like to send the parent object up. I would like for the parent object to include the child and create the child along-side itself in the create section of the parent开发者_如何学编程 controller. The code however is not liking something that I am doing and is failing. I am not sure if this is a problem with naming-convention (IE: the name of the object being passed to rails) or a problem with the linking of the relationships between the models.
Parent Model:
has_many :tags, :as => :taggable
Tag Model:
belongs_to :taggable, :polymorphic => true
Object passed to attempt create:
Parent:
Parent_attributes
tagables_attributes
[0]
'content'
[1]
'content'
...
I have tried passing in the "tabables_attributes" with many different similar names. As I said, I'm not sure if that was the problem-part or not. Perhaps it is the object name that is wrong, or perhaps it is a model problem?
Tag:
t.text :content
t.integer :taggable_id
t.string :taggable_type
Please let me know any ideas as to what I may be doing wrong here.
Add accepts_nested_attributes_for
to your parent model.
See http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
精彩评论