Мой accepts_nested_attributes_for сохраняет только родительские, а не дочерние (вложенные) атрибуты.Это отношения many_to_many между родителем и ребенком.
Проблема: ничего не происходит и ничего не сохраняется.Но на странице я получаю это сообщение:
utf8: "\xE2\x9C\x93"
authenticity_token: 9Pjxt7e5RRgWVGafRyMoDqBSqmtj/R2zBSiVxGGxFOI=
parent: !map:ActiveSupport::HashWithIndifferentAccess
name: "Test"
gender: Male
children_attributes: !map:ActiveSupport::HashWithIndifferentAccess
"0": !map:ActiveSupport::HashWithIndifferentAccess
email: 1234@testing.com
commit: Submit
Из сообщения в моем журнале терминала, я думаю, это потому, что children_attributes не был сохранен, поскольку ему было присвоено «0» ?.Это сообщение терминала:
Started POST "/parents" for 127.0.0.1 at 2011-09-14 11:14:14 -0400
Processing by ParentsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"9Pjxt7e5RRgWVGafRyMoDqBSqmtj/R2zBSiVxGGxFOI=", "parent"=>{"name"=>"123", "gender"=>"Male", "children_attributes"=>{"0"=>{"email"=>"1234@testing.com"}}}, "commit"=>"Submit"}
SQL (0.1ms) BEGIN
SQL (0.6ms) SELECT 1 FROM `children` WHERE (LOWER(`children`.`email`) = LOWER('1234@testing.com')) LIMIT 1
SQL (0.2ms) ROLLBACK
Контроллер-
def new
@parent = Parent.new
@parent.children.build
end
def create
@parent = Parent.new(params[:parent])
if @parent.save
redirect_to root_path
else
render 'new'
end
end
Родительская модель -
attr_accessible :children_attributes
has_many :children, :through => :parent_child_relationships
accepts_nested_attributes_for :children
Дочерняя модель -
has_many :parents, :through => :parent_child_relationships
validates :email, :name, :presence => true
Представление родительской формы -
<%= form_for(@parent, new_parent_path) do |f| %>
<div>
<%= f.label(:name) %></br>
<%= f.text_field(:name) %>
</div>
<div>
<%= f.label(:gender) %> <br/>
<%= f.select(:gender, ['Male', 'Female']) %>
</div>
<%= f.fields_for :children do |ff| %>
<div>
<%= ff.label(:email)%></br>
<%= ff.text_field(:email)%>
</div>
<% end %>
<%= submit_tag "Submit" %>
<% end %>
Любая помощь будет принята с благодарностью!Спасибо!