Rails hidden field undefined method 'merge' error

前端 未结 6 1750
粉色の甜心
粉色の甜心 2020-12-07 14:26

I wanna do something like this in rails

Here is what I have so far in rails:

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


        
相关标签:
6条回答
  • 2020-12-07 14:43

    It works fine in Ruby 1.9 & rails 4

    <%= f.hidden_field :service, value: "test" %>
    
    0 讨论(0)
  • 2020-12-07 14:43

    This also works in Rails 3.2.12:

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

    0 讨论(0)
  • 2020-12-07 14:47

    You should do:

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

    hidden_field expects a hash as a second argument

    0 讨论(0)
  • 2020-12-07 14:49

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

    <%= f.hidden_field :service, value: "test" %>
    
    0 讨论(0)
  • 2020-12-07 14:55

    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 %>
    
    0 讨论(0)
  • 2020-12-07 15:00

    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')
    
    0 讨论(0)
提交回复
热议问题