Can your controller methods route to different pages other than the default in Ruby on Rails

101 views Asked by At

I'm working on a Rails application and I want a form to render with AJAX when I click on a button. I have the routes set up so that when I click the button, rails goes to the NEW method in the corresponding controller but I don't want the controller to load new.html.erb . Instead, is it possible for the NEW controller to process a new.js.erb file? How can I do this? And more generally is it possibly for a method in a controller to route to a view or layout different than the default (for example. method index routes to random.html.erb instead of index.html.erb) Thank you.

1

There are 1 answers

0
Mr H On

Ok here some points that might help you.

You need to add this to the form: remote: true then it will use the Ajax. then in the same folder that you have your view add your new.js.erb.

Your best bet is here: https://github.com/railscasts/136-jquery-ajax-revised

You can see Ryan has made it with and without Ajax. Then you can do the redirection at the Javascript level when response to your Ajax was successful.

Sorry I don't have any code form you so I only can guess what things might look like at your end.

Anyway, here is the tutorial for Railscast http://railscasts.com/episodes/136-jquery-ajax-revised

It is a paid one so you might stick with the GitHub, up to you.

Hope it helps!