Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a rails route to an external URL

A lot of my users keep going to http://(rails app URL)/blog, but I don't actually have a blog. I finally setup a Posterous blog and now want to direct my users there. Is there a way to configure this using routes.rb? Is there a better way that doesn't involve editing the httpd.conf file?

like image 798
Chris Avatar asked Sep 01 '10 22:09

Chris


People also ask

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 do Rails routes work?

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.


2 Answers

I know this is old, so in case someone else needs this for rails 4:

get "/blog" => redirect("http://example.com/blog") 

Use get instead of Match in Rails 4, otherwise you'll get a Runtime error

like image 117
nil Avatar answered Oct 13 '22 23:10

nil


Depends on the Rails version you are using.

Rails 3

# in routes.rb match "/blog" => redirect("http://example.com/blog"), :as => :blog 

Rails 2

# in routes.rb map.blog '/blog',   :controller => "a_helper_controller",   :action => "redirect_to_blog"  # in a_helper_controller.rb def redirect_to_blog   redirect_to "http://example.com/blog" end 
like image 32
Marcel Jackwerth Avatar answered Oct 14 '22 00:10

Marcel Jackwerth