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.
Related
Given the following routes file:
Rails.application.routes.draw do
root to: 'visitors#index'
devise_for :users
resources :users do
resources :wishlists, :only => [:create] do
post :action => :create, :on => :collection
resources :items, :only => [:create, :update, :remove_item] do
post :action => :create, :on => :collection
put :action => :update
delete :action => :remove_item
end
end
end
end
Rails generates routes including the following routes which conflict:
PUT /wishlists/:wishlist_id/items/:item_id(.:format) items#update
wishlist_item PUT /wishlists/:wishlist_id/items/:id(.:format) items#update
Why does the first of these get generated? I would expect only the second one (which includes the path helper)
I'm using Rails 4.1.4
Because your are declaring 2 times the same route:
the first one in resources :items, :only => [:create, :update, :remove_item] generates this resource: /wishlists/:wishlist_id/items/:id(.:format)
the second in put :action => :update generates this one: /wishlists/:wishlist_id/items/:item_id(.:format)
You should be using only 1 of them (I recommend the first one).
If you want more information on routing, you should definitely go on this page.
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.
I'm developing a rails app with an admin section and a public section. In the public section I want to resolve only index and show routes
resources :services, :only => [:index, :show]
However, when I hit the standard URL for the new action it resolves to the show action with an id of 'new'. That is http://foo.com/services/new returns an error "Couldn't find Service with ID=new". Is there anyway I can tell rails NOT to resolve /services/new?
I've already tried
resources :services, :only => [:index, :show], :except => :new
and
resources :services, :except => :new, :only => [:index, :show]
without success.
ETA (by request):
My entire routes file (sans comments):
MyApp::Application.routes.draw do
resources :services, :only => [:index, :show]
resources :packages,:only => [:index, :show]
get "pages/home"
get "pages/about"
get "pages/help"
root :to => 'packages#index'
namespace "admin" do
get "pages/home"
get "pages/about"
get "pages/help"
resources :services
match "/services/:id/add_to_package/:package_id" => "services#add_package", :as => :add_package_to_service, :via => :post, :id => /\d+/, :package_id => /\d+/
match "/services/:id/remove_from_package/:package_id" => "services#remove_package", :as => :remove_package_from_service, :via => :post, :id => /\d+/, :package_id => /\d+/
resources :packages
match "/packages/:id/add_service/:service_id" => "packages#add_service", :as => :add_service_to_package, :via => :post, :id => /\d+/, :service_id => /\d+/
match "/packages/:id/remove_service/:service_id" => "packages#remove_service", :as => :remove_service_from_package, :via => :post, :id => /\d+/, :service_id => /\d+/
resources :users
root :to => 'pages#home'
end
end
You can try to putting a constraint on your :id param
resources :services, :constraints => {:id => /\d+/}, :only => [:index, :show]
This is assuming your :ids are all number based.
I had a similar situation with a redirect vs resource collision, this fixed it.
Well I have the following code for routing with nested resources:
map.resources :cities, :as => "cidade", :only => [:index, :show] do |city|
city.resources :offers, :as => "oferta", :only => [:show], :collection => [:recents], :member => [:share, :buy, :add, :add_gift, :remove, :validate_buy, :want_more, :withdraw_credits], :path_names => { :want_more => "quero-mais", :recents => "recentes", :buy => "comprar", :add_gift => "comprar-presente", :share => "compartilhar", :add => "adicionar", :remove => "remover", :validate_buy => "validar-compra", :withdraw_credits => "resgatar-creditos" } do |offer|
offer.resources :photos, :as => "fotos", :only => [:index]
offer.resources :videos, :as => "videos", :only => [:index, :show]
offer.resources :comments, :as => "comentarios", :only => [:index, :new, :create]
end
end
The thing is I don't want all those ':as =>' to be on the url, by this I mean that I don't want the controllers names on the url, istead of generating /cidades/curitiba/ofertas/1 I only want /curitiba/1.
I've tried :path_prefix and :as => "", but those did not work.
any help?
thanks
What if you try this after the map.resources definition in routes.rb?
match '/:city_name/:id', :to => "offers#show"
Then you do whatever you want in offers_controller#show
I'm currently upgrading my Rails application from 2 to 3. Everything has been going fine until I finished with the routes.rb file. I'll include the contents below:
myApp::Application.routes.draw do
resources :forum_groups
resources :forums do
resources :topics
end
resources :topics do
resources :posts
end
match "confirm_user", :to => "users#confirm"
resources :users do
resources :comments
end
match "moderated_submissions", :to => "submissions#moderated"
resources :submissions do
resources :reviews
end
match "submissions/:id/download" => "submissions#download", :as => :download_submission
match "submissions/:id/trash" => "submissions#trash", :as => :trash_submission
match "submissions/:id/untrash" => "submissions#untrash", :as => :untrash_submission
match "submissions/:id/moderate" => "submissions#moderate", :as => :moderate_submission
match "submissions/:id/unmoderate" => "submissions#unmoderate", :as => :unmoderate_submission
match "submissions/:id/feature" => "submissions#feature", :as => :feature_submission
match "submissions/:id/unfeature" => "submissions#unfeature", :as => :unfeature_submission
#Will work on this later.
#map.user "users/:username", :controller => "users", action => "show"
#map.page "page", :controller => "static_page", :action => "show"
resources :static_pages do
resources :comments
end
resources :categories
resources :comments, :only => [:destroy]
resources :reviews, :only => [:destroy]
resources :features, :as => 'featured'
resources :announcements do
resources :comments
end
root :to => "pages#root"
match "browse", :to => "pages#browse"
match "login", :to => "sessions#new"
match "logout", :to => "sessions#destroy"
match "register", :to => "users#new"
match ":controller/:action/:id(.:format)"
end
The problem occurs when any one of these three lines are present:
resources :comments, :only => [:destroy]
resources :reviews, :only => [:destroy]
resources :features, :as => 'featured'
When any of these lines are present in the routes.rb file I get a, "stack level too deep" error when I try to start the server or the console.
If I remove the statements the application starts fine. If I remove the ":only" and ":as" statements, the application starts fine.
I have tried reordering the routes, and deleting them all except those three and the error still occurs. I have also tried creating a new application from scratch and inserting those three routes. In the case of the new application, it is able to start with those statements.
Any ideas where the problem may lie?