Rails routes match full hostname with multiple period in between - ruby-on-rails

I'm trying to match a URL something like
http://example.com/this.is.my.full.hostname/something/else
apparently when I pass the param in the routes.rb file it doesn't recognize this parameter
my code says the following
match '/:computer_hostname/somethingelse' => 'test#info'
any ideas what's the right way to achieve the URL I wanted above ? Or is it even possible ? I know period is allowed character in URL but does it allow more than one ?

I think the constraints method/option will help you out. Try something like the following:
match ':hostname/something/else' => 'test#info',
:constraints => {:hostname => /[A-Za-z0-9\._\-]+/}
If you're doing multiple matches all with the same :hostname segment, then you can wrap them in a constraints method call:
constraints(:hostname => /[A-Za-z0-9\._\-]+/) do
match ':hostname/something/else' => 'test#info'
match ':hostname/foo/bar' => 'test#foo'
end

Related

Ruby on Rails - Regular expression for error routes

How do I define a routes match any things exclude string ( like 'websocket' )?
Thanks!
Based on the comments, it sounds like you want to match /websocket to a specific action and everything else to an 404 error page.
Utilizing the fact that routes are matched in the order they are defined in routes.rb, this is a good approach to do it:
match '/websocket' => 'controller#action'
match '/:slug' => "errors#show", :code => 404, :via => [:get]
When a request /string comes, the routing subsystem will first try to match it to the first line, and if string is equal to websocket then the match is successful and no more routes will be matched.
If string is not websocket on the other hand, then it will match the second line.

Managing URL Parameters in Rails

Right now I am finding routing and URL constructing within rails to be semi-confusing. I have currently matched the following for tags that are passed in when displaying/filtering data.
match '/posts/standard/' => 'posts#standard'
match '/posts/standard/:tags' => 'posts#standard', :as => :post_tag
match '/posts/standard/:tags' => redirect { |params| "/posts/standard/#{params[:tags].gsub(' ', '+')}" }, :tags => /.+/
However, now I want to add a 'skill' parameter that can only take one state; however, I am very confused by how I want to construct this within my URL. I cannot simply have...
match '/posts/standard/:tags/:skill' => 'posts#standard', as => post_tag, as: => post_skill
So, I am very confused by this at this point, does Rails offer any type of help for constructing URL's?
One way is to just keep your main route
match '/posts/standard/:tags' => 'posts#standard', :as => :post_tag
and handle the additional URL params as params. The url would look like:
/posts/standard/1?skill=something
and it is easy enough to inject the additional params, such as by
link_to post_tag_path(:skill=> 'something')
and your controller would then do
def standard
if params[:skill] == 'something'
...
else
...
end
end
Also, not sure about this, but your first line in your routes 'match '/posts/standard/' => 'posts#standard' may catch all of your routes since there is a match. If this is the case, simply move it to after the first line.

How do I make part of my route optional?

I have this route, which overrides a resource-generated route:
match "make_tiles(/:tile_type,(:finish))" => "tiles#new", :as => :make_tiles
This allows for nice URLs like /make_tiles/two_two,matte
But I'd like the option to use: /make_tiles/two_two also. Currently only /make_tiles/two_two, works.
How can I get rid of the trailing comma requirement?
You can't use a comma to separate fields, and I'm not sure why you'd want to. A comma is not a very good field separator for routes. If you really insist on doing it this way, have the options go into one parameter and separate them manually:
match "make_tiles(/:tile_type_and_finish)" => "tiles#new", :as => :make_tiles
Then in your controller
(tile_type,finish) = params[:tile_type_and_finish].split(",") if params[:tile_type_and_finish].present?
The reason your way isn't allowed is that rails defines the parameter separator as a constant in ActionDispatch::Routing:
SEPARATORS = %w( / . ? )
Otherwise
match "make_tiles(/:tile_type(/:finish))" => "tiles#new", :as => :make_tiles
should work fine.

How to map an optional URL parameter to params[:something] in Rails 3 "routes.rb"?

I have the following line in my config/routes.rb:
match "/assets/:id" => "assets#my_action"
I would like to change it so that both:
/aasets/id
and
/aasets/id/download
will be mapped to assets#my_action, and in case the URL ends with /download I'll have a sign inside my_action (say, params[:something] won't be nil).
How could I do this ?
You can do it in 1 line with :download as an optional parameter e.g.:
match '/assets/:id(/:download)' => "assets#my_action"
Then within your controller params[:download] could be used to check what was after the id in the URL, if anything.
You can simply match both routes to the same controller.
match "/assets/:id" => "assets#my_action"
match "/assets/:id/:downloadparams" => "assets#my_action"
Your check for params[:downloadparams] will handle the rest.
EDIT: Oh "download" won't be a variable right? You could try something like
match "/assets/:id/:action" => "assets#my_action"
Check if params[:action] == "download" and ignore the parameter if it isn't.

Rails Routing Conditional with multiple value options

I have a rails route that is based on two conditions, that the domain and subdomain are a specific value. The problem is that there are multiple possible values for subdomain to work, but I can't seem to be able to pass them as an array or hash.
map.with_options(:conditions => {:domain => AppConfig['base_domain'], :subdomain => 'www'..'www3'}) do |signup|
signup.plans '/signup', :controller => 'accounts', :action => 'plans'
...[truncated]...
end
The above example works as accepting www, www1, www2 & www3 as a value for the subdomain. However, that doesn't really solve my needs. I need to be able to accept a value of '' (nothing), 'www' and 'www2' so I tried something to the extend of:
map.with_options(:conditions => {:domain => AppConfig['base_domain'], :subdomain => ['','www','www2']}) do |signup|
That's similar to how you would set it up in ActiveRecord but it doesn't seem to be the same for routes.
Does anybody know now I can specify three values that aren't sequential?
If you can render it as a regular expression, you can use it as a condition. Converting an array to a regular expression is quite easy:
:subdomain => Regexp.new(%w[ www www3 ].collect { |p| Regexp.escape(p) }.join('|'))
Since you're just dealing with a simple pattern anyway, why not express it as this?
:subdomain => /www\d*/
It is important to note that the regular expressions used by routes are not supposed to be anchored using ^ or $ like you usually would. They must match completely to be valid, and partial matches are ignored.

Resources