Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

thanks a lot for you help. I am having some issues in understanding how fields_for works. I have a location model that has_many :ads. I would like to do a form for filtering locations and ads. I am thinking of using the following syntax:

<%= form_for(@locations) do |f| %>
      <%= f.label :country, "Description" %>
      <%= f.text_field :country, :class => "form-control" %>


then I would use the fields_for(@locations, @locations.ads.build) for those fields that are part of the Ad model, but I am not able to make this work because @locations.ads is an array.

<% fields_for(@locations, @locations.ads.build) do |u| %>
<%= u.label :skill_list, "Skills" %>
<%= u.text_field :skill_list %>
<% end %> 


So I am trying to understand how should I use fields_for with @locations.ads.build?

Could you please give me any advice, explanation and help?
I am doing this because I want to do @locations = Location.all, the filter the ads based on the @location.country and @ads.skill_list

Thanks a lot
Fabrizio

What I have tried:

I have been searching online for explanation and I thought of posting a question about this
Posted
Updated 6-Mar-17 2:44am

1 solution

Ok. I found out the solution using nested attributes hear is the link to the explanation.

Ruby on Rails, two models in one form - Stack Overflow[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900