Dmitriy
Dmitriy

Reputation: 341

Rails 5: how to respond_to js after redirect?

On good old rails 4.2 I used to submit my remote: true form via ajax

$.ajax
   type: 'POST'
   url: post_url
   data: $("##{instance_type}-modal-form").serialize() + '&redirect_url=' + redirect_url
   dataType: 'script'

In my controller i was redirecting if redirect_url is present

if params[ :redirect_url ]
  format.html { redirect_to params[ :redirect_url ] }
  format.js { redirect_to params[ :redirect_url ] }
...

And I used to be redirected to new.js.coffee template. But now, on 5.0.0.1 it redirects to HTML

    Started PATCH "/call_journals/111" for 127.0.0.1 at 2016-08-21 00:02:06 +0300
Processing by CallJournalsController#update as JS
.........

Redirected to http://localhost:3000/partners/new?call_journal_id=111
Completed 200 OK in 33ms (ActiveRecord: 3.1ms)

Started GET "/partners/new?call_journal_id=111" for 127.0.0.1 at 2016-08-21 00:02:06 +0300
Processing by PartnersController#new as HTML
  Parameters: {"call_journal_id"=>"111"}

What do I do to get this fixed?

Upvotes: 1

Views: 874

Answers (1)

Pradeep Sapkota
Pradeep Sapkota

Reputation: 2072

Solution

After redirect you cannot use format.js for user response. Because to use javascript response your application needs to get ajax request or remote call without redirecting page. But you can use json format.

Upvotes: 1

Related Questions