Add this line to your application's Gemfile:
And then execute:
Or install it yourself as:
$ gem install turbolinks-redirect
This approach assumes that you are trying to get turbolinks to play nice with rails_ujs and want your ajax forms to be able to "redirect" on success.
//= require turbolinks //= require turbolinks.redirect
Now, write your controller actions as you normally would:
class PostsController < ActionController::Base def create @post = Post.new params[ :post ] if @post.save redirect_to @post else render :new end end end
Under the hood,
redirect_to checks to see if the request is an XHR. If
it is, instead of responding with a 3xx status code, it responds with a
custom 2xx status code, still setting the Location header.
Then, a handler is attached to the rails_ujs "ajax:success" event. It checks for this particular status code, and if it finds it, tells turbolinks to visit the page located in the header.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature)
- Commit your changes (
git commit -am 'Add some feature')
- Push to the branch (
git push origin my-new-feature)
- Create new Pull Request