关联对象未使用 fields_for 保存
我是 Rails 新手,遇到关联对象未保存的问题。我想我已经按照应该做的方式做了一切,但我不明白为什么它不起作用。因此,提前感谢所有可以帮助我更接近解决这个问题的人。 这些是我的模型:
class Examdate < ActiveRecord::Base
belongs_to :exam
attr_accessible :date, :exam_id
end
class Exam < ActiveRecord::Base
attr_accessible :title, :prof_id, :deadline
belongs_to :prof
has_many :examdates, :dependent => :destroy
accepts_nested_attributes_for :examdates
end
在我的 exams_controller
中,我有这个:
def new
@exam = Exam.new
3.times{@exam.examdates.build()}
end
def create
@exam = Exam.new(params[:exam])
respond_to do |format|
if @exam.save
....
现在在我看来,我有 semantic_fields_for
方法,我也用正常的 fields_for
尝试过它并得到了相同的结果:
<%= semantic_form_for @exam do |f| %>
<% if @exam.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@exam.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @exam.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.inputs do%>
<%= f.input :title%>
<%= f.input :prof%>
<%= f.input :deadline, :start_year => Time.now.year, :label => "Anmeldefrist"%>
<% end %>
<%= f.semantic_fields_for :examdates do |builder|%>
<%= render "examdates_fields", :f => builder %>
<% end %>
<%= f.buttons do %>
<%= f.commit_button "Speichern"%>
<% end %>
<% end %>
在部分中是这样的,稍后将进行扩展
<%= f.inputs :date%>
现在我得到了包含正确的三个日期字段的表单,并且我可以正确保存 Exam
本身。当我查看 params[:exam][:examdates_attributes] 时,日期就在那里:
{"0"=>{"date(1i)"=>"2006", "date(2i)"=>"1", "date(3i)"=>"1"},
"1"=>{"date(1i)"=>"2006", "date(2i)"=>"1", "date(3i)"=>"1"},
"2"=>{"date(1i)"=>"2006", "date(2i)"=>"1", "date(3i)"=>"1"}}
但是当我将 Exam.find(1).exdates
放入 Rails 控制台时,我获取[]
。我真的不知道我做错了什么,所以非常感谢每一个小提示:)
i´m a newbie to rails and have a problem with my associated object not being saved. I think I did everything the way it should be done and I can´t figure out, why it isn´t working. So thanks in advance for everyone who can help me get a little closer to solving this problem.
These are my models :
class Examdate < ActiveRecord::Base
belongs_to :exam
attr_accessible :date, :exam_id
end
class Exam < ActiveRecord::Base
attr_accessible :title, :prof_id, :deadline
belongs_to :prof
has_many :examdates, :dependent => :destroy
accepts_nested_attributes_for :examdates
end
In my exams_controller
I have this:
def new
@exam = Exam.new
3.times{@exam.examdates.build()}
end
def create
@exam = Exam.new(params[:exam])
respond_to do |format|
if @exam.save
....
Now in my view I have the semantic_fields_for
method, I also tried it with normal fields_for
and got the same result:
<%= semantic_form_for @exam do |f| %>
<% if @exam.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@exam.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @exam.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.inputs do%>
<%= f.input :title%>
<%= f.input :prof%>
<%= f.input :deadline, :start_year => Time.now.year, :label => "Anmeldefrist"%>
<% end %>
<%= f.semantic_fields_for :examdates do |builder|%>
<%= render "examdates_fields", :f => builder %>
<% end %>
<%= f.buttons do %>
<%= f.commit_button "Speichern"%>
<% end %>
<% end %>
In the partial is this, will later be extended
<%= f.inputs :date%>
Now I get the form with the correct three date fields and I can save the Exam
itself correctly. When I look at params[:exam][:examdates_attributes]
the dates are there:
{"0"=>{"date(1i)"=>"2006", "date(2i)"=>"1", "date(3i)"=>"1"},
"1"=>{"date(1i)"=>"2006", "date(2i)"=>"1", "date(3i)"=>"1"},
"2"=>{"date(1i)"=>"2006", "date(2i)"=>"1", "date(3i)"=>"1"}}
But when I put Exam.find(1).exdates
in my rails Console, I get []
. I really don´t have any idea what I did wrong, so every little tip is very appreciated:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您在 Exam 模型中使用
attr_accessible
,我认为您必须在该列表中包含:examdates_attributes
。否则,将不允许对嵌套模型进行批量分配。Since you are using
attr_accessible
in your Exam model, I think you'll have to include:examdates_attributes
in that list. Otherwise, mass assignment to the nested model will not be allowed.