Using Rails link_to for links that post

后端 未结 5 1032

I have a link that I need to submit a post request with. Normally, I\'d use jQuery and prevent the link\'s default behavior and then submit a form to the destination. This s

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-02 14:34

    Note that if the user has JS disabled or you have removed the unobtrusive JS libraries that come by default, link_to will be silently submitted via a GET request.

    In general, I am not very fond of having links that perform POST requests. I think that's the role of a form and a button.

    Thus, an easy (and safer) alternative is to use the Rails button_to helper:

    button_to 'Profile', profile_path(@profile, param1: 'value1', param2: 'value2')
    

    button_to also supports the method option but as it defaults to post so I've just omitted it.

提交回复
热议问题