[Solved-4 Solutions] Rails hidden field undefined method 'merge' error



Error Description:

Rails hidden field undefined method 'merge' error

Solution 1:

    • We should do:
    <%= f.hidden_field :service, :value => "test" %>
    click below button to copy the code. By - ruby on rails tutorial - team
    • hidden_field expects a hash as a second argument
    • we are using a hidden_field instead of a hidden_field_tag. Because we are using the non-_tag version, it is assumed that our 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>
      
      click below button to copy the code. By - ruby on rails tutorial - team

      view:

        <%= form_for @order do |f| %>
          <%= f.hidden_field :service %>
          <%= f.submit %>
        <% end %>
        
        click below button to copy the code. By - ruby on rails tutorial - team

        Solution 2:

          We should do:

          <%= f.hidden_field :service, :value => "test" %>
          
          click below button to copy the code. By - ruby on rails tutorial - team
          • hidden_field expects a hash as a second argument

          Solution 3:

            • By the way, we don't use hidden fields to send data from server to browser. Data attributes are awesome. We can do
            <%= form_for @order, 'data-service' => 'test' do |f| %>
            
            click below button to copy the code. By - ruby on rails tutorial - team
            • And then get attribute value with jquery
            $('form').data('service')
            
            click below button to copy the code. By - ruby on rails tutorial - team

            Solution 4:

              • A version with the new syntax for hashes in ruby 1.9:
              <%= f.hidden_field :service, value: "test" %>
              
              click below button to copy the code. By - ruby on rails tutorial - team

              Related Searches to Rails hidden field undefined method 'merge' error