Rails隐藏字段未定义的方法“合并”错误

时间:2021-12-01 17:42:51

I wanna do something like this in rails

我想在rails中做一些类似的事情。

Here is what I have so far in rails:

这是我到目前为止在rails上看到的:

<%= form_for @order do |f| %>
  <%= f.hidden_field :service, "test" %>
  <%= f.submit %>
<% end %>

But then I get this error:

然后我得到了这个错误

undefined method `merge' for "test":String

How can I pass values in my hidden_field in rails?

如何在rails中的hidden_field中传递值?

6 个解决方案

#1


405  

You should do:

你应该做的是:

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

hidden_field expects a hash as a second argument

hidden_field希望散列作为第二个参数

#2


53  

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:

您正在使用一个hidden_field而不是一个hidden_field_tag。因为您正在使用non-_tag版本,所以假定您的控制器已经在支持表单的对象上设置了该属性的值。例如:

controller:

控制器:

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

view:

观点:

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

#3


26  

It works fine in Ruby 1.9 & rails 4

它在Ruby 1.9和rails 4中运行良好

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

#4


3  

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

ruby 1.9中新的散列语法版本:

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

#5


2  

This also works in Rails 3.2.12:

这也适用于Rails 3.2.12:

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

< % = f。hidden_field:service,:value =>“test”%>

#6


0  

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

然后用jquery获取属性值。

$('form').data('service')

#1


405  

You should do:

你应该做的是:

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

hidden_field expects a hash as a second argument

hidden_field希望散列作为第二个参数

#2


53  

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:

您正在使用一个hidden_field而不是一个hidden_field_tag。因为您正在使用non-_tag版本,所以假定您的控制器已经在支持表单的对象上设置了该属性的值。例如:

controller:

控制器:

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

view:

观点:

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

#3


26  

It works fine in Ruby 1.9 & rails 4

它在Ruby 1.9和rails 4中运行良好

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

#4


3  

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

ruby 1.9中新的散列语法版本:

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

#5


2  

This also works in Rails 3.2.12:

这也适用于Rails 3.2.12:

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

< % = f。hidden_field:service,:value =>“test”%>

#6


0  

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

然后用jquery获取属性值。

$('form').data('service')