Rails Route Constraint - ruby-on-rails

match '/:question_id', :to => 'welcome#dashboard', :via => [:get], constraint: { question_id: /\d+/ }
I don't know why this routing rule is matching paths like
localhost/assets
because I added the numeric constraint to only matching digits.

The option is contstraints.
match '/:question_id', :to => 'welcome#dashboard', :via => [:get], constraints: { question_id: /\d+/ }

Related

how do I ignore a term in a route?

I'm creating a route that should have an optional/ignored last term.
Like so:
/product/12345/Dark-Knight-Rises # last term is just there for a nice URL
I was thinking, from reading the docs, that I'd just be able to wildcard the last term:
match 'product/:uid/*full_name' => 'product#view', :via => [:get]
That didn't work. I did get this to work:
match 'product/:uid/:full_name' => 'product#view', :via => [:get]
match 'product/:uid' => 'product#view', :via => [:get]
But, well, it seems like this should be doable in one line. Yes?
match 'product/:uid(/:full_name)' => 'product#view', :via => [:get] is what you are looking for
The parenthesis make the full_name an optional parameter which you can just ignore since all you want is a pretty URL.
Below single line should work
match 'product/:uid/:full_name' => 'product#view', :via => [:get]

Rails routing, matching short urls?

I'm having an issue where I've created a route I'm using to match short token like urls, like this:
myapp.com/a2c3b
I'm doing that by using a route like this:
match '/:id' => 'items#show', :as => "show_item", :via => :get, :constraints => { :id => /[a-z0-9]{5}/ }
But the issue is that now my other routes like /admin don't work because that also has 5 characters, how can I work around this, and have both kinds of routes work?
Put all of your routes that would match before this route in the file... that is...
match '/admin'....
match '/login'....
match '/:id' => 'items#show', :as => "show_item", :via => :get, :constraints => { :id => /[a-z0-9]{5}/ }

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 =>
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
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

How do you create a route to a specific show item?

I would like 'about' to route to 'abouts/1'
I tried this:
match 'about' => 'abouts#show/1', :via => get
and it doesn't work. Any ideas?
How about:
match 'about' => 'abouts#show', :via => :get, :defaults => {:id => 1}
What about just removing the 1 from the route and retrieve the record you want directly in the controller method?
# routes.rb
match 'about' => 'abouts#show', :via => get
# abouts_controller.rb
def show
#about = About.find(1)
end

Allowing Underscore in Username Routes in Rails app

Just wondering how I can edit my routes file to allow routing of usernames with an underscore character "_" in them. Here's the relevant code:
controller :users do
match ':id' => :show, :via => :get, :constraints => { :id => /[A-Za-z0-9\-\+]+/ }
end
Update your regular expression to add an _ as follows:
/[A-Za-z0-9\-\_\+]+/
The completed match will look like this:
match ':id' => :show, :via => :get, :constraints => { :id => /[A-Za-z0-9\-\_\+]+/ }
The construction A-Za-z0-9\_ is the same as \w. So we can use shorter version:
/[\w\-\+]+/
I think this regex pattern doesn't ignore this test string:
XXXX XXXXX
It has space between string.
If you update this regex pattern it will ignore this that test string:
controller :users do
match ':id' => :show, :via => :get, :constraints => { :id => /^[\w\-\+]+$/ }
end

Resources