Jeni
Jeni

Reputation: 1118

Rails 'remote:true' ignored when params provided

In a Rails (5.2.4.2) app

I am using button_to + remote: true, and this is working as expected -> ajax call fired, all is OK.

But then I add 'params' to the button_to. Params are added to the form as hidden input (as expected) but when I click on the button, the request made is not remote, and all the page content is updated.

Problematic: button_to + remote + params -> this code seems to ignore the remote:true, although I see data-remote="true" in the form tag

<%= button_to ent_path(ent),
      {remote: true,
      method: :patch,
      class:"btn btn-primary",
      params: {ent: {active_a: false}}
    } do
%>
  <span>TEEEST</span>
<% end %>

Working as expected:

<%= button_to ent_path(ent),
      {remote: true,
      method: :patch,
      class:"btn btn-primary",
    } do
%>
  <span>TEEEST</span>
<% end %>

So my purpose is to have the button_to remote and update the ent record, changing the active_a attribute depending on some logic

Upvotes: 0

Views: 211

Answers (2)

Jeni
Jeni

Reputation: 1118

Turned out the remote:true was working properly. But there was a redirect_back in the controller that was executed in case of some custom logic, so that appeared as if the button is working without remote:true

Upvotes: 0

scilence
scilence

Reputation: 1115

You want to pass any extra parameters via the link path, like this

<%= button_to ent_path(ent, active_a: false),
      {remote: true,
      method: :patch,
      class:"btn btn-primary",
    } do
%>
  <span>TEEEST</span>
<% end %>

Upvotes: 2

Related Questions