accepts_nested_attributes_for keeps form fields from showing

三世轮回 提交于 2019-12-11 04:44:30

问题


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


回答1:


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.




回答2:


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:

<= f.fields_for @survey.questions.build do |question_fields| %>


来源:https://stackoverflow.com/questions/4729672/accepts-nested-attributes-for-keeps-form-fields-from-showing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!