Accepts_nested_attributes_for 阻止表单字段显示
当我使用 accepts_nested_attributes_for
时,相应的字段不再显示在我的视图中。
class Survey < ActiveRecord::Base
has_many :questions
accepts_nested_attributes_for :questions
end
class Question < ActiveRecord::Base
belongs_to :survey
end
然后在我看来:
<%= form_for @survey do |f| %>
<%= f.fields_for :questions do |question_fields| %>
<%= question_fields.text_area :text %>
<% end %>
<% end %>
如果我删除 accepts_nested_attributes_for
那么 text_area
显示,但如果我保留它......什么都不会渲染。
我正在运行 Rails 3.0.3
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否在控制器中构建了问题?
类似于
@survey.questions.build
这会构建一个相关问题,因此只会显示一个文本区域。 这样的循环运行它,
像
2.times { @survey.questions.build }
它将出现 2 次。
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.
您想要创建新问题还是要编辑它们?如果您为此调查创建新问题,您可能需要尝试如下操作:
Do you want to create new questions or are you editing them? You might want to try something like this if you are creating a new question for this survey: