Moon
Moon

Reputation: 22565

Route depending on a session value?

Is it possible to route a request depending on a session value?

I would like to route /test to test#withoutsession or /test to test#withsession depending on a session value.

Upvotes: 2

Views: 76

Answers (1)

Marcelo De Polli
Marcelo De Polli

Reputation: 29291

Your best bet is probably applying dynamic constraints to your routes, like this:

get '/test', :to => 'test#withoutsession', :constraints => lambda{ |req| req.session[:user_id].blank? }
get '/test', :to => 'test#withsession', :constraints => lambda{ |req| req.session[:user_id].present? }

Reference: http://edgeguides.rubyonrails.org/routing.html#advanced-constraints

Upvotes: 1

Related Questions