将局部变量传递给部分中的hidden_​​field

发布于 2024-08-20 21:04:47 字数 1045 浏览 5 评论 0原文

我试图将这些本地参数传递给部分表单,并且它们被传递给隐藏字段。我无法将这些值直接放入表格中,因为它们会发生变化。任何帮助将不胜感激。

我有:

    <% form_for :user, @user, :url => { :action => "create", :controller => "users"} do |f| %>
      <%= f.error_messages %>
      <%= render :partial => "form", :locals => { :f => f, :position => 'Company Commander', :battalion_id => @battalion.id, :company_id => c.id} %>
      <%= f.submit "Register" %>
    <% end %>

_form.html

<%= f.hidden_field (:position, :value => :position) %>
<%= f.hidden_field (:battalion_id, :value => @battalion.id) %>
<%= f.hidden_field (:battalion_id, :value => @company.id) %>

<%= f.hidden_field (:roles, :value => "Company") %>

<%= f.label (:name, "Name:") %>
<%= f.text_field :name%>
<br />
<%= f.label (:login, "Login:") %>
<%= f.text_field :login %>
<br />
<%= f.label (:email, "E-Mail:") %>
<%= f.text_field :email%>

I am trying to pass these local parameters to a partial form, and they are being passed to hidden_fields. I can't put the values straight into the forms because they will change. Any help would be greatly appreciated.

I have:

    <% form_for :user, @user, :url => { :action => "create", :controller => "users"} do |f| %>
      <%= f.error_messages %>
      <%= render :partial => "form", :locals => { :f => f, :position => 'Company Commander', :battalion_id => @battalion.id, :company_id => c.id} %>
      <%= f.submit "Register" %>
    <% end %>

The _form.html

<%= f.hidden_field (:position, :value => :position) %>
<%= f.hidden_field (:battalion_id, :value => @battalion.id) %>
<%= f.hidden_field (:battalion_id, :value => @company.id) %>

<%= f.hidden_field (:roles, :value => "Company") %>

<%= f.label (:name, "Name:") %>
<%= f.text_field :name%>
<br />
<%= f.label (:login, "Login:") %>
<%= f.text_field :login %>
<br />
<%= f.label (:email, "E-Mail:") %>
<%= f.text_field :email%>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑梦风尘 2024-08-27 21:04:47

你在这里混合了两个想法。

首先,

实例变量(名称以“@”开头的变量)可用于所有视图和局部视图。它有效,但被认为是不好的做法。特别是在处理可以从多个操作调用的部分时。

无论如何,您的代码没有理由不能工作。即使您在 locals 参数中没有 company_id 和 battalion_id 。

其次,

您似乎误解了 :locals 参数的工作原理。当使用给定 :locals 的散列调用 render 时,Rails 将为给定散列中的每个键/值对定义一个局部变量。每个局部变量将具有哈希键的名称和哈希值的值。

以您的代码为例:

<%= render :partial => "form", :locals => { :f => f, :position => 'Company Commander', :battalion_id => @battalion.id, :company_id => c.id} %>

这将渲染一个可以访问以下局部变量的部分:fpositionbattalion_id 和 < code>company_id 分别设置为 f、'Company Commander'、@battalion.id、@company.id 的当前值。

PS 您有两个 battalion_id 隐藏字段。只有第一个才会被使用。看起来第二个应该是company_id。

You're mixing two ideas here.

First,

Instance variables (variables who's names start with an '@') are available to all views and partials. It works, but it's considered bad practice. Particularly when dealing with partials that can be called from multiple actions.

In any case, there's no reason your code shouldn't work. Even if you didn't have the company_id and battalion_id in the locals argument.

Second,

It seems that you have misunderstood how the :locals argument works. When render is called with a hash given to :locals, Rails will define a local variable for each key/value pair in the given hash. Each local variable will have the name the hash key and the value of the hash value.

To use your code as an example:

<%= render :partial => "form", :locals => { :f => f, :position => 'Company Commander', :battalion_id => @battalion.id, :company_id => c.id} %>

This will render a partial that has access to the following local variables: f, position, battalion_id and company_id set to the current values of f, 'Company Commander', @battalion.id, @company.id respectively.

P.S. You have two hidden fields for battalion_id. Only the first one will ever be used. It looks like the second one should be company_id.

沐歌 2024-08-27 21:04:47

当您将 :locals 参数用于部分时,您可以在引用变量时省略 @

<%= f.hidden_field :battalion_id, :value => battalion_id %>

When you use the :locals argument for a partial, you can omit the @ when referencing the variable:

<%= f.hidden_field :battalion_id, :value => battalion_id %>
彩虹直至黑白 2024-08-27 21:04:47

在你的部分,尝试......

<%= f.hidden_field (:position, :value => position) %>
<%= f.hidden_field (:battalion_id, :value => battalion_id) %>
<%= f.hidden_field (:battalion_id, :value => company_id) %>

In your partial, try…

<%= f.hidden_field (:position, :value => position) %>
<%= f.hidden_field (:battalion_id, :value => battalion_id) %>
<%= f.hidden_field (:battalion_id, :value => company_id) %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文