Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment-specific routing in Rails 3

I'd like to create a scope in my routes.rb file to only route certain URLs when in the production environment. How do you use conditionals with Rails 3 routing? All I need to do is restrict those rules based on Rails.env.production? being true, but I'm not sure of the syntax.

like image 436
Devin Avatar asked Jul 07 '11 16:07

Devin


People also ask

How do I find a specific route in Rails?

Decoding the http request TIP: If you ever want to list all the routes of your application you can use rails routes on your terminal and if you want to list routes of a specific resource, you can use rails routes | grep hotel . This will list all the routes of Hotel.

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.

What is the difference between resources and resource in Rails?

Difference between singular resource and resources in Rails routes. So far, we have been using resources to declare a resource. Rails also lets us declare a singular version of it using resource. Rails recommends us to use singular resource when we do not have an identifier.


1 Answers

routes.rb is Ruby file so this should work:

  if Rails.env.production?
    get "/bar" => 'welcome#index'
  else
    get "/foo" => 'welcome#index'
  end
like image 123
Casual Coder Avatar answered Sep 28 '22 09:09

Casual Coder