Multiple Nested Routes, is there a better way to do this? - ruby-on-rails

So In my rails app I have two resources (rentals, and reservations) which belong to a user. This is the code in my routes.rb to set up the nested routes.
map.resources :users, :has_many => :reservations, :shallow => true
map.resources :users, :has_many => :rentals, :shallow => true
map.resources :rentals, :only => [:index]
map.resources :reservations, :only => [:index]
Is there a more perferred way to do this. I've done some googling but I can't find a clear answer.
Thanks in advance.
-ray

Your method duplicates the routes for users, as you can see by running rake routes. You can fix that by passing a block to map.resources:
map.resources :users, :shallow => true do |user|
user.resources :reservations
user.resources :rentals
end
The nested routes created will assume that you always want to access those resources in a nested fashion.
If you really need all the routes you've defined (including the non-nested rentals and reservations index) then you will need to add:
map.resources :rentals, :only => [:index]
map.resources :reservations, :only => [:index]
And I don't know of a DRYer way to do that.

Nests the two resources under users:
map.resources :users, :shallow => true do |users|
users.resources :reservations, :only => :index
users.resources :rentals, :only => :index
end
Edit: Sorry, forgot the :shallow option.

You can define nested routes with blocks
map.resources :users, :shallow => true do |user|
user.resources :reservations, :only => [:index]
user.resources :rentals, :only => [:index]
end
I feel that this way is a bit more clear and can more easily be adjusted later when you need additional options on one of the nested resources.
The different options and the details are at the ActionController Resources API page

Related

Resource route members. Plural url with singular method name?

I have the following resource block
resources :projects, :except => [:destroy] do
resources :tasks, :except => [:index]
get "tasks/:id/change_state" => "tasks#change_state", :as => "task_change_state"
get "tasks/:id/assign_user" => "tasks#assign_user", :as => "task_assign_user"
get "tasks/:id/unassign" => "tasks#unassign", :as => "task_unassign"
end
I'm wondering how I can refactor those routes a bit without touching the path. I tried doing the following:
resources :projects, :except => [:destroy] do
resources :tasks, :except => [:index] do
member do
get 'change_state'
get 'assign_user'
get 'task_unassign'
end
end
end
But that leaves the route method names with tasks as plural. I need the URL to be plural, much like in the original get method call. But I need the method name to be singular, as I would prefer to not change the implementation across the app.

Rails routes with polymorphic and owner relationships

I have a rails app where Users can create Programs and other things yet to come, we'll say Plans (hypothetically).
Users can also like Programs and Plans. (Like is polymorphic)
I am trying to figure out routes so I can see the Programs a User creates, all the things a User likes, or just Programs or just Plans a User likes.
I have these routes, but this doesn't seem to be right.
resources :users, :only => [:index,:show] do
resources :programs, :only => [:index]
resources :likes, :only => [:index] do
resources :programs, :only => [:index]
end
end
The route for programs that a user likes requires a URL like: users/user_id/likes/like_id/program.
How do I create a URL like: users/user_id/likes/programs and get all programs that are liked.
I am using the Socialization Gem which has a method for "likeables_relation(Class)" which returns a relation of whatever class is requested. I just need help with the routing.
I've just tried with the following routes:
resources :users, :only => [:index,:show] do
resources :programs, :only => [:index]
resources :likes, :only => [:index] do
collection do
get :programs
end
end
end
and the
http://localhost:3000/users/1/likes/programs
works fine.
It required adding a new action 'programs' to the LikesController class.
This is what rake routes returns:
programs_user_likes GET /users/:user_id/likes/programs(.:format) likes#programs

add new routes to a resource without the added param?

I would like to add a url to my comments route so I can call "post_comments_latest_path". I added something like 'get "comments/latest" => "comments#latest", :as => "latest"' but the route adds and the :commend_id to the path that is not needed. Any suggestions?
resources :posts, :except => [:index] do
resources :comments, :except => [:index, :show] do
post "replies" => "comments#create_reply", :as => "create_reply"
get "replies/new" => "comments#new_reply", :as => "new_reply"
end
end
This should work:
resources :posts, :except => [:index] do
resources :comments, :except => [:index, :show] do
post "replies" => "comments#create_reply", :as => "create_rely"
get "replies/new" => "comments#new_reply", :as => "new_reply"
get "latest", :on => "collection"
end
end
A Member route is one that links to a specific resource; requires an id.
A Collection route is one that links to a resource collection; does not require an id.
See the Rails Routing Guide for more information.

resource available output parent resource rails 3.2

I have this in my routes.rb
resources :users, :path => "/", :only => [:show] do
resources :collections, :controller => 'users/collections'
end
Why can I access to collections resources from:
http://localhost:3000/en/collections
if this resouce this inside users?
I want that collection resource only is enable inside:
http://localhost:3000/en/users/collections
This is my routes:
user_collections GET (/:locale)/:user_id/collections(.:format) users/collections#index
POST (/:locale)/:user_id/collections(.:format) users/collections#create
new_user_collection GET (/:locale)/:user_id/collections/new(.:format) users/collections#new
edit_user_collection GET (/:locale)/:user_id/collections/:id/edit(.:format) users/collections#edit
user_collection GET (/:locale)/:user_id/collections/:id(.:format) users/collections#show
PUT (/:locale)/:user_id/collections/:id(.:format) users/collections#update
DELETE (/:locale)/:user_id/collections/:id(.:format) users/collections#destroy
How can I do it?
Thank you
Try this:
resources :users, :only => [:show] do
resources :collections, :controller => 'users/collections'
end

routing urls and paths in rails

I have a controller which has a method called history
class UsersController < ApplicationController
def history
User.return_history(params[:id])
end
end
I have the following in my routes.rb file
map.resources :users, :shallow => true do |user|
user.resources :friends, :shallow => false
user.resources :posts, :collection=>{:no_access => :get}
user.resources :photos
end
How do I try to Ajax call the history method of the users_controller.rb? Using link_to_remote in the following way
link_to_remote 'History', :url=>history_user_path(#user), :update=>"history", :method=>'get'
throws me an error saying history_user_path() not found. How can this be? edit_user_path() shows no error and edit is not even explicitly defined in the User.rb file. Thanks.
mapresources :users creates a bunch of url/path helper methods, including edit_users_path. If you need others. you've got to add it as either a :member, or :collection option for map.resources.
This will let you do what you want:
map.resources :users, :shallow => true, :member => {:history => :get} do |user|
user.resources :friends, :shallow => false
user.resources :posts, :collection=>{:no_access => :get}
user.resources :photos
end

Resources