Ruby on rails fields_for表单 [英] Ruby on rails fields_for form

查看:83
本文介绍了Ruby on rails fields_for表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



非常感谢您的帮助。我在理解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,一种形式的两个模型 - Stack Overflow [ ^ ]
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[^]


这篇关于Ruby on rails fields_for表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆