在不使用 FormBuilder 的情况下访问 fields_for 内的嵌套模型属性 [英] Accessing nested model attributes inside a fields_for without using FormBuilder

查看:42
本文介绍了在不使用 FormBuilder 的情况下访问 fields_for 内的嵌套模型属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Rails 表单,用于创建和编辑具有 has_many 关系的模型.我非常熟悉具有嵌套模型的典型表单,但我当前的问题需要使用典型的 FormBuilder 帮助程序访问呈现一些表单元素,并使用模型本身的数据访问其他 HTML 元素.例如,我的顶级表单类似于:

I have a Rails form that is being used for creating and editing a model with a has_many relationship. I'm very familiar with typical forms with nested models, but my current problem requires accessing rendering some form elements using typical FormBuilder helpers and other HTML elements using data from the model itself. For example, my top level form has something like:

<% customer_form.fields_for :customer_images do |images_form| %>
    <%= render :partial => 'customer_image_show', :locals => { :f => images_form } %>
<% end %>

然后,在表单partial中,我需要做一些类似的事情:

Then, in the form partial, I need to do something like:

<dd><%= f.text_field :image_description %></dd>

... 但也可以访问来自 customer_images 模型的属性(例如,customer_image 记录的 ID).

... but also access attributes from the customer_images model (for example, the ID of the customer_image record).

我觉得这应该很简单,我只是缺少一些基本的东西.任何帮助表示赞赏.这是一个 Rails 2.3.8 应用程序.

I feel like this should be pretty straightforward and I'm just missing something basic. Any help is appreciated. This is a Rails 2.3.8 application.

推荐答案

可以拨打

f.object

获取与表单关联的对象.

to get to the object that the form is associated with.

这篇关于在不使用 FormBuilder 的情况下访问 fields_for 内的嵌套模型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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