Petros Kalafatidis
Petros Kalafatidis

Reputation: 765

send post params with a link_to in rails

I want to call an api endpoint on my rails app. I cannot find the way to pass post params from link_to nor from form_tag

link_to '/v1/my_endpoint/approve', data: { confirm: "Are you sure?" }, remote: true, method: :post, id: 'approve-id' do 'link_name' end

I want together with the above link to pass some post params.

Upvotes: 0

Views: 48

Answers (1)

Jon
Jon

Reputation: 10918

link_to isn't going to do what you need. You could pass query params that way, but not form params.

You need to use a form, or write some javascript to submit the request for you.

Rails has a handy button_to helper which will create a small form, presented in your UI as a single button. You can add params to that quite easily:

<%= button_to "button label", "/v1/my_endpoint/approve", remote: true, params: { id: "approve-id", something: "else" } %>

If you wanted, you could use CSS to style that button as a link.

You can read about the button_to method in the docs here: https://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to

Upvotes: 1

Related Questions