f.fields_for a has_many relationship in rails -


i have assessment model has_many answers. i'm trying build f.fields_for getting error:

"undefined method `content' <answer::activerecord_associations_collectionproxy:0x007fdb12041fd8>" 

view:

 <%= f.fields_for @answers |builder| %>    <%= builder.text_area :content, :class=>"form-control question-field", :data => {:question => question.id} %>   <% end %> 

controller:

  def edit     @assessment = current_user.assessments.find(params[:id])     @answers = @assessment.answers   end 

i understand error seems i'm calling methods on collection rather individual object. don't understand how fix it.

if f builder @assassment, can do:

<%= f.fields_for :answers |builder| %>   <%= builder.text_area :content, :class=>"form-control question-field", :data => {:question => question.id} %> <% end %> 

you need in assassement model:

class assasement < activerecord::base   has_many :answers   accepts_nested_attributes_for :answers   #... end 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -