Route to Controller and Set Index - ruby-on-rails

I'm doing the following in my routes.rb file:
match '/projects/:project_id/research/worksheet/:action' => 'worksheets/research'
What this route doesn't match, however, is /projects/whatever/research/worksheet => worksheets/research#index. I'm forced to add another line:
match '/projects/:project_id/research/worksheet' => 'worksheets/research#index'
Is there a better way to accomplish this?

Related

How to create rails route with constrants for numbers-only that accepts empty parameter?

I have created some route like this:
get 'foo/:offset' => 'foo#action', :as => :foo, :constraint => { id: /\d+/ }
It works fine, but: I want rails to route /foo to foo#action if no parameter specified, so not only urls like /foo/123 will be routed but simple /foo too.
How can I change constraint for this? Thanks for help!
I don't know if this is the best solution, but it's a simple one:
Create a route for /foo, and another for /foo/:offset to the same controller and action!

Rails routes match starting with a numeric character

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

Rails routes match full hostname with multiple period in between

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

named routing doesnt work in rails3

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.

Rails hide controller name

i have match ":id" => "people#show" in my routes.rb
now i can access http://localhost:3000/1
but,in views <%= link_to 'Show', people %> it will generate http://localhost:3000/people/1 ,
i want to it to be http://localhost:3000/1
You could do something like this to ensure that only numeric ids are matched:
match '/:id' => 'people#show', :constraints => {:id => /\d+/}
A good alternative might be to use some kind of identifier, even if it's not the controller name: http://localhost:3000/p/1. This will at least ensure that if you add other controllers and actions you don't end up having to change your routing structure.
You could write a custom route to match that in config/routes.rb. At the bottom of your routes.rb file you will have a route like match ':controller(/:action(/:id(.:format)))'
or something like resources :people. You might have to write a route that matches the route type you want.
You have to create a named route.
match ':id' => 'people#show', :as => :person
And fix your views to use your new method person_path(user_id).

Resources