For some strange reason cardsets_path('my') generates "/cardsets.my" instead of "/cardsets/my". Please explain why?
config/routes.rb:
match '/cardsets/:type', :to => 'cardsets#index', :requirements => { :type => /my|public/ }, :as => 'cardsets'
resources :users do
resources :cardsets do
end
end
rake routes:
cardsets /cardsets/:type(.:format) {:controller=>"cardsets", :action=>"index"}
Shouldn't it be
cardsets_path(:type => 'my')
However, type is a reserved word in rails.
Related
I want to have something like this:
get '/received/:resouce' => 'received#index'
get '/received/:resouce/:resouce_id' => 'received#show'
post '/received/:resouce' => 'received#create'
put '/received/:resouce' => 'received#update'
delete '/received/:resouce' => 'received#delete'
get '/sent/:resouce' => 'sent#index'
get '/sent/:resouce/:resouce_id' => 'sent#show'
post '/sent/:resouce' => 'sent#create'
put '/sent/:resouce' => 'sent#update'
delete '/sent/:resouce' => 'sent#delete'
But this is very verbose. I want to use the scope and resources methods of routes for rails.
Any suggestions?
Got it!
scope '/sent/:resource' do
resources :sent, param: :resource_id, path: '/'
end
scope '/received/:resource' do
resources :received, param: :resource_id, path: '/'
end
I feel like I should know this and I'm certain this could be done more cleanly, but I'm not quite sure the best way to go about it.
How could a set of routes like this be written in a more DRY way?
# Artists
match "/:id/remixes", :to => "artists#remixes", :as => "artist_remixes"
match "/:id/originals", :to => "artists#originals", :as => "artist_originals"
match "/:id/popular", :to => "artists#popular", :as => "artist_popular"
match "/:id/mashups", :to => "artists#mashups", :as => "artist_mashups"
match "/:id/covers", :to => "artists#covers", :as => "artist_covers"
match "/:id/productions", :to => "artists#productions", :as => "artist_productions"
match "/:id/features", :to => "artists#features", :as => "artist_features"
That should do it:
resources :artists, path: '/' do
member do
get 'remixes'
get 'originals'
get 'popular'
get 'mashups'
get 'covers'
get 'features'
end
end
I would look to try and do 1 route and pass list_type as a parameter.
Something like
resources: artists do
resources list_types
end
I would try and avoid having seperate actions for a bunch of methods that probably do similar things.
Ah, should have just thought this through (hungover today):
[:remixes_of, :remixes_by, :originals, :popular, :mashups, :covers, :productions, :features].each do |role|
match ":id/#{role}", to: "artists\##{role}", as: "artist_#{role}"
end
I want to route http://localhost:3000/users/1/rename/alex to my users controller with rename action.
what I did was:
match 'users/:id/rename/:name' => 'users#rename', but this is not working, the part after 'users/:id/' is not mapped at all, since I cannot get name by params[:name]
Update:
In routes.rb
resources :users do
put 'rename/:code', :action => :rename, :code => /\w{5}/, :on => :member
end
and,
$ rake routes
...
PUT /users/:id/rename/:code(.:format) {:code=>/\w{5}/, :action=>"rename", :controller=>"users"}
...
If you have resources :users, put your match line before it.
Alternatively, you can pass a block to resources:
resources :users do
match 'rename/:name' => 'users#rename', :on => :member
end
I already have a route to match /username for the users show page. However, when I add another action, for example: followers and following as below:
resources :users, :only => [:show] do
get :following, :followers
end
I get the URL /users/username/following instead of /username/following.
How can I make all the /users/username URL's be matched as /username/etc.. ?
Here's my /username route:
match '/:id' => 'users#show', :constraints => { :id => /[a-zA-Z0-9\-_]*/ }, :as => "user_profile"
thank you.
Edit:
I have fixed this by adding
match '/:id/following' => 'users#show/following', :as => "user_following"
match '/:id/followed' => 'users#show/following', :as => "user_followers"
But I'm not sure if that's the best solution. What I'd really want is a default route that would match all /:id/:action to the appropriate action omitting the /users.
I have fixed this by adding
match '/:id/following' => 'users#show/following', :as => "user_following"
match '/:id/followed' => 'users#show/following', :as => "user_followers"
But I'm not sure if that's the best solution. What I'd really want is a default route that would match all /:id/:action to the appropriate action omitting the /users.
Near the bottom
match ':id/:action', :controller => :users
Generally I have MenuItems model and trying to make '/menu_items(/:id(:/some_action))' URLs looks like '/menu(/:id(:/some_action))'
In Rails 2.3.5 it was
map.resources :menu_items, :as => :menu, :path_names => { :new => 'add' }
Now in Rails 3.0.3 I'm able to handle it using this huge paragraph of code
resources :menu_items, :path_names => { :new => 'add' }
match 'menu/' => 'menu_items#index', :as => :menu
match 'menu/add' => 'menu_items#new', :as => :new_menu
match 'menu/:id' => 'menu_items#show', :as => :show_menu
match 'menu/:id/edit' => 'menu_items#edit', :as => :edit_menu
But it looks incorrect because of huge amount of code.
Seems :as works like 2nd Rails' map.some_name now.
Any help/suggestions/guides? (Thanks)
http://guides.rubyonrails.org/routing.html#customizing-resourceful-routes
resources :menu, :controller => "menu_items", :path_names => { :new => "add" }
Output is quite close to what you're after:
menu_index GET /menu(.:format) {:controller=>"menu_items", :action=>"index"}
POST /menu(.:format) {:controller=>"menu_items", :action=>"create"}
new_menu GET /menu/add(.:format) {:controller=>"menu_items", :action=>"new"}
edit_menu GET /menu/:id/edit(.:format) {:controller=>"menu_items", :action=>"edit"}
menu GET /menu/:id(.:format) {:controller=>"menu_items", :action=>"show"}
PUT /menu/:id(.:format) {:controller=>"menu_items", :action=>"update"}
DELETE /menu/:id(.:format) {:controller=>"menu_items", :action=>"destroy"}
Another way
resources :menu, :as => :menu_items, :controller => :menu_items
This approach has the advantage that the exiting helper methods such as menu_items_path still work.