Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing a Rails route based on deployment type

Is there a good way of modifying a route based on the deployment type?

Basically, I have a route that has a :requirements => {:protocol => "https"}, and I'd like that to only happen in production, but not in development.

like image 982
Steve Klabnik Avatar asked Oct 05 '09 19:10

Steve Klabnik


People also ask

What is match in Rails routes?

Rails routes are matched in the order they are specified, so if you have a resources :photos above a get 'photos/poll' the show action's route for the resources line will be matched before the get line. To fix this, move the get line above the resources line so that it is matched first.

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 I find a specific route in Rails?

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.


1 Answers

You can explicitly define them separately and test for the environment

  if Rails.env.production?
    map.resources :purchases, :requirements => {:protocol => "https"}
  else
    map.resources :purchases
  end

Note, if you're on older versions of Rails, use ENV['RAILS_ENV'] == production instead

like image 142
semanticart Avatar answered Sep 29 '22 16:09

semanticart