How to avoid resolving a route in rails? - ruby-on-rails

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.

Related

Ruby on Rails (Redmine) add route

Please help me add the custom route in format:
/projects/any-project-name/estimated_time/report
controller: estimatedtime
method: report
routes.rb
Rails.application.routes.draw do
root :to => 'welcome#index', :as => 'home'
.....
get '/projects/:project_id/issues/gantt', :to => 'gantts#show', :as => 'project_gantt'
get '/issues/gantt', :to => 'gantts#show'
.....
resources :time_entries, :controller => 'timelog', :except => [:show, :edit, :update, :destroy] do
get 'report', :on => :collection
end
.....
resources :time_entries, :controller => 'timelog', :only => [:new, :create]
.....
end
Try this out: get 'projects/:project_name_attribute/estimated_time/report' => 'estimatedtimes#report'

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.

Check my Ruby on Rails routes.rb file

I just recently started programming in Ruby on Rails, and I was wondering if some of you could look over my routes.rb file that I am using so far and tell me if I am over thinking this.
I am aware of the whole RESTful approach in RoR and I am trying to stick to it, but I am not sure if I am on track. So far my application only has the following functionality:
User registration
User activation (via email link)
User can request activation to be resent
User log in
User log out
User requests password reset (gets an email)
Basic UCP (change email and password)
I am using a lot of redirect_to *_url and *_path, so I want a lot of named routes. I am trying to explicitly declare only routes that are allowed. Thanks for your input.
MyApp::Application.routes.draw do
get 'home' => 'pages#index', :as => 'home'
get 'testing' => 'pages#testing', :as => 'testing'
get 'register' => 'users#new', :as => 'register'
post 'users/create'
resources :users, :only => [
:new,
:create
]
get 'activation' => 'activations#new', :as => 'activation'
get 'activate/:token' => 'activations#activate', :as => 'activate'
post 'activations/edit'
resources :activations, :only => [
:new,
:activate,
:edit
]
get 'login' => 'sessions#new', :as => 'login'
get 'logout' => 'sessions#destroy', :as => 'logout'
get 'sessions/destroy'
resources :sessions, :only => [
:new,
:create,
:destroy
]
get 'forgot_password' => 'resets#new', :as => 'forgot_password'
post 'resets/create'
get 'activate_password/:token' => 'resets#activate', :as => 'activate_password'
put 'save_password' => 'resets#save', :as => 'save_password'
resources :resets, :only => [
:new,
:create,
:activate,
:save
]
get 'ucp' => 'ucp#show', :as => 'ucp'
post 'ucp_update' => 'ucp#update', :as => 'ucp_update'
resources :ucp, :only => [
:show,
:update
]
root :to => 'pages#index'
end
When you use resources, it automatically makes named routes for you. I won't go through your entire routes file, but one example:
get 'activation' => 'activations#new', :as => 'activation'
get 'activate/:token' => 'activations#activate', :as => 'activate'
post 'activations/edit'
resources :activations, :only => [
:new,
:activate,
:edit
]
Could be:
resources :activations, :only => [:new, :edit] do
get 'activate', :on => :member
end
which will produce new_activation_path, edit_activation_path, and activate_activation_path
Go to the Rails Routing Guide for a lot of cool stuff you can do in routes. For example, if you want to use "register" instead of "new" for your Users paths:
resources :users, :only => [:new, :create], :path_names => [:new => 'register']

Rails 2.3.8 routing, how do I create a route without a controller name on nested resources?

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

Rails 3: Stack level too deep when using routes with ":only" or ":as"

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?

Resources