Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have multiple constraints in Rails routes.rb?

I would like a rails route that takes 2 constraints into consideration. How can this be done? The two constraints

match ':id' => 'pages#temp', :constraints => { :uuid => /[A-Za-z\d]([-\w]{,498}[A-Za-z\d])?/ }
root :to => 'pages#temp', :constraints => lambda {|r| r.env["warden"].authenticate? }

How can I have one route like so with both of the constraints in place? Thanks

match ':id' => 'pages#temp', :constraints =>
like image 299
AnApprentice Avatar asked Jun 27 '12 21:06

AnApprentice


2 Answers

I guess you will have to make a custom constraints class and put all your constraints there. Refer the advanced constraints in rails guides(Link below) for more information.

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

like image 78
udit mittal Avatar answered Sep 18 '22 15:09

udit mittal


I had to use multiple constraints for subdomains and usernames. I used a block to solve the issues:

constraints subdomain: ['survey', 'survey.staging'] do
    match "/(:username)", to: "responses#index", constraints: { username: /[0-z\.\-\_]+/ }, :via => [:get, :post]
end

So you might try something like this:

constraints id: { uuid: /[A-Za-z\d]([-\w]{,498}[A-Za-z\d])?/ } do
  match '/:id' to: 'pages#temp', constraints: lambda {|r| r.env["warden"].authenticate? }
end
like image 20
Mark Avatar answered Sep 18 '22 15:09

Mark