How to add additional params to a button_to form?

2020-02-16 08:11发布

I want to have a Submit button. It updates one field on the submission; submission.state = :submitted

Now, I could make a custom route and a custom action and just post to that. But that seems really heavy-handed. Especially since I'll also have a reject button and possibly more. Needing a custom route & action for each of those seems downright silly to me.

It would be much nicer if I could do something like

button_to "Submit", submission_url(submission), :method => :put, :submission => { :state => :submitted }

Which would post to the submission's update method and update only the desired field.

But that doesn't work. How can I make it work? Or do you have a better idea of how to do this?

7条回答
戒情不戒烟
2楼-- · 2020-02-16 08:43

As of Rails 3.2.1 you can add additional params to the :html_options hash using the :form key.

http://apidock.com/rails/v3.2.1/ActionView/Helpers/UrlHelper/button_to

This did not exist prior to 3.2.1 so the more verbose solution of declaring a form with hidden attributes was required.

查看更多
登录 后发表回答