I have a an url like "http://domain.com/1and2" that I wanted to set up in config/routes.rb like this:
match "1and2" => "frontpage#oneandtwo"
(with controllers and views in place).
Running 'rake routes' outputs 'Invalid route name: '1and2''. This error is apparently triggered when you start a match with a numeric character.
Is there a workaround, or am I doing it wrong?
match '/:id' => "frontpage#oneandtwo", :constraints => {:id => /1and2/}
The root of the problem is that methods in Ruby cannot start with a number. Since Rails routing will generate an accessor method for each route, you'll get an error.
You can pass by the issue by naming your route differently with the :as parameter.
I had an issue where I wanted to redirect from a URI /2012 -- which resulted in an error. I corrected it by adding :as => current_year to the routing:
match "/#{Time.now.year}" => redirect("..."), :as => :current_year
Further information:
https://github.com/rails/rails/issues/3224
Related
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.
Okay, so I've upgraded to Rails 4 (kind of unplanned with my 10.9 server update) and have been able to get everything running on my photo gallery app except for the routes. For some reason I've always had trouble understanding routes since rails 3. Here was my previous working code under Rails 3
root :to => "gallery#index", :as => "gallery"
get 'gallery' => 'gallery#index'
resources :galleries
match 'gallery_:id' => 'gallery#show', :as => 'gallery'
I understand that match has been depreciated, but if I try to use GET, I'm getting the following error:
Invalid route name, already in use: 'gallery' You may have defined two routes with the same name using the :as option, or you may be overriding a route already defined by a resource with the same naming.
Basically, I want the root (index) to load as "/photos/gallery" as it does, and my show action to load, for example, record id 435 as: "/photos/gallery_435" which is how I previously had it working. Sorry for what is probably a simple question, I just cannot seem to grasp the rails routing.
Try this
match 'gallery_:id' => 'gallery#show', :via => [:get], :as => 'gallery_show'
You can then refer to this path as gallery_show_path in your helpers and views.
Changing the 'as' removes the conflict.
I'm building a Rails application which lets users have like a personal folder where they can upload files or make another folders.
How could I make a route that could be simmilar to
/user/:id/files/:any_path
and matches routes like this
/user/2/files/some_folder/a_file.doc
You can try to match a custom route like this:
match '/user/:id/files/*any_path'
This route would match /user/2/files/3 or /user/2/files/long/path/to/3, setting params[:any_path] to "3" or "long/path/to/3".
Starting from Rails 3.1, wildcard routes will always match the optional format segment by default. For example if you have this route:
match '*pages' => 'pages#show'
By requesting "/foo/bar.json", your params[:pages] will be equals to "foo/bar" with the request format of JSON. If you want the old 3.0.x behavior back, you could supply :format => false like this:
match '*pages' => 'pages#show', :format => false
If you want to make the format segment mandatory, so it cannot be omitted, you can supply :format => true like this:
match '*pages' => 'pages#show', :format => true
This info and more details you can find on Rails routing from the outside in
I looked on the web for a while but I can't get this to work. Our application has to work with urls like ourapp.com/meandyou, where the common element is the "and" in the parameter.
I saw that it's possible to constrain urls parameters using regex, so I added the rule to routes.rb, but without success. If I try to match the same expression using the terminal, it works. Here's the complete route file:
Railroot::Application.routes.draw do
resources :couples
get "home/index"
root :to => 'home#index'
match ':url' => 'couples#show_url', :url => /and/
end
I read that Rails nests the expression within a bigger one when matching the route, so maybe I'm doing something slightly wrong even for such a simple expression.
I'm running on Ubuntu 10.04, Ruby 1.9.3, Rails 3.2.3, Passenger 3.0.13, Nginx 1.2.1.
Thanks in advance for your help!
This should be your starting point:
Railroot::Application.routes.draw do
root :to => 'home#index'
resources :couples
match ':url' => 'couples#show_url', :constraints => { :url => /and/ }
end
This may be your answer, from the rails routing docs:
:constraints takes regular expressions with the restriction that regexp anchors can’t be used. [...]
However, note that you don’t need to use anchors because all routes are anchored at the start.
So I think what you are actually matching against is not /and/ but /^and/, which would explain why it's not working.
Try being more explicit, like this:
match ':url' => 'couples#show_url', :url => /.*and/
Hey, I want to name my route using the :as parameter. Ive read the Rails Routing Guide about this but unfortunately it wont display me /my_courses in the url
match 'course_enrollments', :to => 'course_enrollments#index', :as => 'my_courses'
thx for your time!
match 'my_courses', :to => 'course_enrollments#index', :as => 'my_courses'
This will route /my_courses to the index action of your CourseEnrollments controller, and allow you to refer to the path by referencing my_courses_path or my_courses_url in your views and controllers.
To clarify: The first parameter in match is what maps the route to an actual URL. The :as option simply allows you to override the name of the route helper.
That matches course_enrollments in the URL, not my_courses. The :as parameter means you can refer to the route in views using (in this example) my_courses_path.