Is there a way to pass params when clicking submit button in simple_form view in rails 3.2.12?

后端 未结 3 1894
时光取名叫无心
时光取名叫无心 2020-12-05 18:09

In simple_form view, the submit button is like this:

<%= f.button :submit, \'Save\' %>

We are trying to pass a params subaction

相关标签:
3条回答
  • 2020-12-05 18:20

    Use name and value option.

       <%= f.button  :submit , name: "subaction",value: "update"%>
    

    In your controller you will get params[:subaction] with the value "update"

    0 讨论(0)
  • 2020-12-05 18:38

    By specifying f.button :button, type: 'submit', we can use the name and value attributes as follows to submit using a single param. Notably, the value submitted (e.g., 'cake') may be different from the button text (e.g., 'The Best Cake').

    _form.html.erb

    <%= f.button :button, 'The Best Cake', type: 'submit', name: 'dessert_choice', value: 'cake' %>
    <%= f.button :button, 'The Best Pie', type: 'submit', name: 'dessert_choice', value: 'pie' %>
    

    Controller

    def controller_action
      dessert_choice = params[:dessert_choice] # 'cake' or 'pie'
    end
    

    This approach avoids the need for hidden inputs as @dax mentioned in the comment above.

    Tested on Simple Form 3.3.1 with Rails 4.2.

    0 讨论(0)
  • 2020-12-05 18:47

    as dax points out,

    <%= hidden_field_tag(:subaction, 'update') %>
    <%= f.button :submit, 'Save' %>
    

    This will provide the string value 'update' to the routed controller action via the hidden field. It can then be retrieved by the controller with

    params[:subaction]
    
    0 讨论(0)
提交回复
热议问题