Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to create this redirecting route in Rails?

Is it possible to do a redirect in the routes file of a Rails app?

Specifically, I'd like to forward /j/e to /javascripts/embed.js

Right now the only way I can think to do it is to create a j controller with an e method that redirects to that.

like image 884
Shpigford Avatar asked Mar 25 '10 21:03

Shpigford


People also ask

How do I redirect in Rails?

Rails's redirect_to takes two parameters, option and response_status (optional). It redirects the browser to the target specified in options. This parameter can be: Hash - The URL will be generated by calling url_for with the options.

How many types of routes are there in Rails?

Rails RESTful Design which creates seven routes all mapping to the user controller. Rails also allows you to define multiple resources in one line.

How does routing work in Rails?

Rails routing is a two-way piece of machinery – rather as if you could turn trees into paper, and then turn paper back into trees. Specifically, it both connects incoming HTTP requests to the code in your application's controllers, and helps you generate URLs without having to hard-code them as strings.


1 Answers

In Rails 4 and 5: (thanks @dennis)

get '/stories', to: redirect('/posts') 

In Rails 3, you can redirect inside the routes.rb file.

match "/posts/github" => redirect("http://github.com/rails.atom") 
like image 189
Steven Soroka Avatar answered Sep 21 '22 04:09

Steven Soroka