Rails hidden field undefined method 'merge' error

ⅰ亾dé卋堺 提交于 2019-11-28 03:30:37

You should do:

<%= f.hidden_field :service, :value => "test" %>

hidden_field expects a hash as a second argument

user132447

You are using a hidden_field instead of a hidden_field_tag. Because you are using the non-_tag version, it is assumed that your controller has already set the value for that attribute on the object that backs the form. For example:

controller:

def new
  ...
  @order.service = "test"
  ...
end</pre>

view:

<%= form_for @order do |f| %>
  <%= f.hidden_field :service %>
  <%= f.submit %>
<% end %>
Tushar.PUCSD

It works fine in Ruby 1.9 & rails 4

<%= f.hidden_field :service, value: "test" %>

A version with the new syntax for hashes in ruby 1.9:

<%= f.hidden_field :service, value: "test" %>

This also works in Rails 3.2.12:

<%= f.hidden_field :service, :value => "test" %>

By the way, I don't use hidden fields to send data from server to browser. Data attributes are awesome. You can do

<%= form_for @order, 'data-service' => 'test' do |f| %>

And then get attribute value with jquery

$('form').data('service')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!