accep_nested_attributes_for保持表单域不显示 [英] accepts_nested_attributes_for keeps form fields from showing
问题描述
当我使用 accepts_nested_attributes_for
时,相应的字段不再显示在我的视图中。
class Survey< ActiveRecord :: Base
has_many:questions
accept_nested_attributes_for:questions
end
class问题< ActiveRecord :: Base
belongs_to:survey
end
然后在我看来:
<%= form_for @survey do | f | %GT;
<%= f.fields_for:questions do | question_fields | %GT;
<%= question_fields.text_area:text%>
<%end%>
<%end%>
如果我删除 accepts_nested_attributes_for
,那么 text_area
显示,但如果我保留它...没有任何呈现。
我正在运行Rails 3.0.3
您是否在控制器中构建问题?
类似于
@ survey.questions.build
这会建立一个相关的问题,因此只会显示一个文本区域。像循环一样运行它。
2.times {@ survey.questions.build}
它会出现2次。
When I use accepts_nested_attributes_for
the corresponding fields no longer show in my view.
class Survey < ActiveRecord::Base
has_many :questions
accepts_nested_attributes_for :questions
end
class Question < ActiveRecord::Base
belongs_to :survey
end
Then in my view:
<%= form_for @survey do |f| %>
<%= f.fields_for :questions do |question_fields| %>
<%= question_fields.text_area :text %>
<% end %>
<% end %>
If I remove accepts_nested_attributes_for
then the text_area
shows, but if I keep it...nothing gets renders.
I'm running Rails 3.0.3
Did you build the questions , in the controller ?
Something like
@survey.questions.build
This builds one related question, so only one text area will show up. run it in a loop like
2.times { @survey.questions.build }
It will appear 2 times.
这篇关于accep_nested_attributes_for保持表单域不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!