Creating a rails route to an external URL

Ruby on-RailsRouting

Ruby on-Rails Problem Overview


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?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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

Solution 2 - Ruby on-Rails

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

Solution 3 - Ruby on-Rails

For Rails 5:

get '/stories', to: redirect('/articles')
get '/stories', to: redirect('http://google.com')

Rails Guide source page

Solution 4 - Ruby on-Rails

Emulate frontend routes as regular Rails controller routes

Background : at the beginning there was Rails monolith rendering html view. Then came a frontend React app, and the need to convert the backend to a JSON api, and generate URLs (mostly in emails) pointing to a frontend app that behaves almost exactly like a Rails controller.

I was looking for a way to mimick the rails resource way to construct URL but to point instead to a frontend URL and generate appropriate path_helpers, that could be used easily with my app, RSpec, cucumber/Capybara, and everything else. I found this hack around routes to emulate frontend routes, which also requires an extra param to be passed to completely desambiguate frontend VS backend routes (useful in capybara testing)

frontend_scope = {
  as: :frontend,
  host: Rails.configuration.frontend_host, # like https://www.example.com
  port: Rails.configuration.frontend_port, # 443
  constraints: (lambda { |request| request.params[:app] == :frontend })
}
scope(frontend_scope) do
  root to: 'static_pages_controller#home'
  resources :articles, only: [:show, :index]
end

Then in my Ruby code I can use

frontend_article_url(@article, app: :frontend)
frontend_articles_url(app: :frontend)
... (and everything that can be generated in a classic rails app)

The inconvenient is that there will be a app parameter in your redirection, that you would have to ignore on your frontend app + all other web apps like Google Analytics, Search engine, etc*.

* the problem being, if you have both a /articles on your frontend and a /articles on your backend, your app will not be able to make the difference with both routes that resolve to the same URL without the help from an extra param. If you are converting your fullstack app to a Rails API, or if you also need the same routes to handle prerendering/meta for SEO, this can be a problem, otherwise this should be largely cancelled by a /api/ prefix

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChrisView Question on Stackoverflow
Solution 1 - Ruby on-RailsnilView Answer on Stackoverflow
Solution 2 - Ruby on-RailsMarcel JackwerthView Answer on Stackoverflow
Solution 3 - Ruby on-RailsvitkozView Answer on Stackoverflow
Solution 4 - Ruby on-RailsCyril Duchon-DorisView Answer on Stackoverflow