问题描述
您好,
非常感谢您的帮助。我在理解fields_for如何工作方面遇到了一些问题。我有一个has_many:ads的位置模型。我想做一个过滤位置和广告的表单。我正在考虑使用以下语法:
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" %>
然后我会使用fields_for(@locations,@ locations.ads.build)作为广告模型一部分的字段,但由于@ locations.ads是一个数组,我无法使其工作。
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 %>
所以我想了解我应该如何在@ locations.ads.build中使用fields_for?
你能给我任何建议,解释和帮助吗?
我这样做是因为我想做@locations = Location.all,过滤广告基于@ location.country和@ ads.skill_list
非常感谢
Fabrizio
我尝试了什么:
我一直在网上搜索解释,我想发布一个关于这个
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
推荐答案
这篇关于Ruby on rails fields_for表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!