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

后端 未结 3 1901
时光取名叫无心
时光取名叫无心 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: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.

提交回复
热议问题