将局部变量传递给部分中的hidden_field
我试图将这些本地参数传递给部分表单,并且它们被传递给隐藏字段。我无法将这些值直接放入表格中,因为它们会发生变化。任何帮助将不胜感激。
我有:
<% 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你在这里混合了两个想法。
首先,
实例变量(名称以“@”开头的变量)可用于所有视图和局部视图。它有效,但被认为是不好的做法。特别是在处理可以从多个操作调用的部分时。
无论如何,您的代码没有理由不能工作。即使您在 locals 参数中没有 company_id 和 battalion_id 。
其次,
您似乎误解了 :locals 参数的工作原理。当使用给定 :locals 的散列调用 render 时,Rails 将为给定散列中的每个键/值对定义一个局部变量。每个局部变量将具有哈希键的名称和哈希值的值。
以您的代码为例:
这将渲染一个可以访问以下局部变量的部分:
f
、position
、battalion_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:
This will render a partial that has access to the following local variables:
f
,position
,battalion_id
andcompany_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.
当您将
:locals
参数用于部分时,您可以在引用变量时省略@
:When you use the
:locals
argument for a partial, you can omit the@
when referencing the variable:在你的部分,尝试......
In your partial, try…