Ruby on Rails - 如何将哈希参数传递给局部视图?

时间:2010-08-31 03:51:30

标签: ruby-on-rails partial-views

'shared / subscription'%>

要调用此部分视图:

<% form_for(:subscription, :url => city_subscriptions_path(@city)) do |form| %>
  <%= form.hidden_field :city_id, :value => @city.id %>
  <%= form.text_field :email, :size => 30 %>
  <%= form.submit "Email Me" %>
<% end %>

由于我在不同的地方使用这个局部视图,我如何更改调用者以便它将传递form_for助手的哈希值?所以当调用帮助程序时会是这样的:

<% form_for(:subscription, :url => city_subscriptions_path(@city), :html => {:id => 'main_input' }) do |form| %>
  <%= form.hidden_field :city_id, :value => @city.id %>
  <%= form.text_field :email, :size => 30 %>
  <%= form.submit "Email Me" %>
<% end %>

1 个答案:

答案 0 :(得分:6)

<%= render :partial => "shared/subscription", :locals => {:foo => "bar", :foofoo => ["bar", "bar"]}

在局部视图中,使用它们:

<%= foo #this outputs "bar" %>
<%= foofoo.to_s %>