开发者

How to pass a Ruby hash key and value to a Rails FormHelper function?

开发者 https://www.devze.com 2023-01-19 22:25 出处:网络
In the controller, I\'d like to do: @options = { :obj_id => @obj.id, :obj2_id => @obj2.id } And in the view:

In the controller, I'd like to do:

@options = { :obj_id => @obj.id, :obj2_id => @obj2.id }

And in the view:

<%=
remote_form_for(:model_object, :url => { :action => 'some_action' }) do |f|
     @options.each { |k, v|
       f.hidden_field(k, { :value => v })
     }
}
%>

The code above currently will just output the string val开发者_C百科ues of each key/value and not a hidden input field.

My experience tells me I'm missing something simple... What am I missing?

Thanks for the help.


You don't need to use send for this because hidden_field isn't a private method, nor is the method you're calling dynamic. These are the only two reasons you should be using send.

Instead, make your form use more ERB tags:

<%= remote_form_for(:model_object, :url => { :action => 'some_action' }) do |f| %>
  <% @options.each do |k, v| %>
    <%= f.hidden_field(k, { :value => v }) %>
  <% end %>
<% end %>


Right after I posted the question and thought about it more thoroughly (and with some luck), I found the object.send function. The following code resolves my issue:

In the controller:

@options = { :obj_id => @obj.id, :obj2_id => @obj2.id }

In the view:

 <% remote_form_for(:model_object, :url => { :action => 'some_action' }) do |f| %>
      <% @options.each { |k, v| %>
        <%= f.send :hidden_field, k, { :value => v } %>
      <% } %>
 }
 %>
0

精彩评论

暂无评论...
验证码 换一张
取 消