https://github.com/remind101/turbolinks-redirect
Simple redirect_to support for turbolinks and jquery-rails.
https://github.com/remind101/turbolinks-redirect
Last synced: over 1 year ago
JSON representation
Simple redirect_to support for turbolinks and jquery-rails.
- Host: GitHub
- URL: https://github.com/remind101/turbolinks-redirect
- Owner: remind101
- License: mit
- Created: 2013-04-11T17:33:52.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2013-08-17T01:33:56.000Z (almost 13 years ago)
- Last Synced: 2025-04-05T20:01:43.790Z (over 1 year ago)
- Language: Ruby
- Size: 52.7 KB
- Stars: 9
- Watchers: 93
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Turbolinks::Redirect
Simple `redirect_to` support for [turbolinks][turbolinks] and
[jquery-rails][jquery-rails].
## Installation
Add this line to your application's Gemfile:
gem 'turbolinks-redirect'
And then execute:
$ bundle
Or install it yourself as:
$ gem install turbolinks-redirect
## Usage
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.
Simply require `turbolinks.redirect` in your javascripts:
``` javascript
//= require turbolinks
//= require turbolinks.redirect
```
Now, write your controller actions as you normally would:
``` ruby
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.
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
[turbolinks]: https://github.com/rails/turbolinks/
[jquery-rails]: https://github.com/rails/jquery-rails