John
John

Reputation: 634

Redirects in rails

I have a controller action: create which is responding to json format and html.

respond_to do |format|
  if @pr.save
    format.html {redirect_to(some_path)}
    format.json {render json: @pr, status: :ok}
  else
    format.html {}
    format.json {}
  end
end

So from a angular service I send a post request to this action, if it is successful I would like to redirect the user to some_path in my server log I see that redirect bing made, but the page is not changing.

How can I fix this? I am using turbo links as well so I would like not loading all the files again.

Upvotes: 1

Views: 60

Answers (2)

SSR
SSR

Reputation: 6438

You should have a route where you want to redirect your user. Then In controller You can call the page

if( <Your Condition true/false> )
   $location.path('/dashboard');

remember you should have added dependency $location in your controller Or you can take a look here :

How to integrate Rails views with angularjs ng-view?

Upvotes: 0

alexsmn
alexsmn

Reputation: 1746

If you are using Turbolinks you can do:

  1. Set a location in your controller:

    format.json {render json: @pr, status: :ok, location: some_path}

  2. Get the location from your header response in js:

    header('Location')

  3. Use Turbolinks visit:

    Turbolinks.visit(header('Location'));

And now you are not loading the assets.

Upvotes: 1

Related Questions