Rails route which disappears in application but is present in rake routes - ruby-on-rails

I have a mystic problem....
In my routes.rb I have some routes defined and for exemple
resources :projects, :except => [:destroy] do
get :edit_flyer, :on => :member
get :guests, :on => :member
end
If I run a rake routes, I get
edit_flyer_project GET /projects/:id/edit_flyer(.:format) {:controller=>"projects", :action=>"edit_flyer"}
guests_project GET /projects/:id/guests(.:format) {:controller=>"projects", :action=>"guests"}
GET /projects(.:format) {:controller=>"projects", :action=>"index"}
projects POST /projects(.:format) {:controller=>"projects", :action=>"create"}
new_project GET /projects/new(.:format) {:controller=>"projects", :action=>"new"}
GET /projects/:id(.:format) {:controller=>"projects", :action=>"show"}
project PUT /projects/:id(.:format) {:controller=>"projects", :action=>"update"}
edit_project GET /projects/:id/edit(.:format) {:controller=>"projects", :action=>"edit"}
As you can see, the show action is defined.
But in my rails applications, the route show is not defined.
I add this in my application controller just to monitored the routes.
before_filter :zba
def zba
ActionController::Routing::Routes.named_routes.routes.each do |name, route|
puts "%20s: %s" % [name, route]
end
exit
end
And it appears that the route action is not defined ....
Then, I tryed to clean my routes.rb, like removing all my back namespace, and magically it works.
It seems to be a bug, or I don't know what appened.
Have you any idea how to debug this ? I also tryed to remove plugin/gems. No change.
I run with Rails3.rc with ruby 1.8.7 !
Thanks for your help !

Try this
resources :projects, :except => [:destroy] do
member do
get :edit_flyer
get :guests
end
end

Related

Administrate - Rails 4 | `add_route': Invalid route name, already in use: 'admin_root'

=> After deploying my rails app on Heroku
=> I cannot access to my admin at mysite.heroku.com/admin
--
I run heroku logs
The error : Invalid route name, already in use: 'admin_root'
/app/vendor/bundle/ruby/2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/routing/route_set.rb:549:in `add_route': Invalid route name, already in use: 'admin_root' (ArgumentError)
And You may have defined two routes with the same name using the :as option
2016-10-24T13:43:56.930010+00:00 app[web.1]: You may have defined two routes with the same name using the `:as` option, or you may be overriding a route already defined by a resource with the same naming. For the latter, you can restrict the routes created with `resources` as explained here:
--
This is my config/routes.rb :
Rails.application.routes.draw do
root 'pages#index'
get '/agence' => 'pages#agence'
get '/methode' => 'pages#methode'
get 'projets' => 'projet#index'
get 'projets/:slug' => 'projet#show', as: 'projet'
get '/article' => 'article#index'
get '/article/:slug' => 'article#show', as: 'articles'
get '/contact' => 'pages#contact'
get '/mentionslegales' => 'pages#mentionslegales'
namespace :admin do
resources :projets
resources :articles
resources :users
# get '/' => 'projets#index'
root to: "projets#index"
end
if defined?(DashboardManifest)
namespace :admin do
DashboardManifest::DASHBOARDS.each do |dashboard_resource|
resources dashboard_resource
end
root controller: DashboardManifest::ROOT_DASHBOARD, action: :index
end
end
--
Rake routes :
Prefix Verb URI Pattern Controller#Action
root GET / pages#index
agence GET /agence(.:format) pages#agence
methode GET /methode(.:format) pages#methode
projets GET /projets(.:format) projet#index
projet GET /projets/:slug(.:format) projet#show
article GET /article(.:format) article#index
articles GET /article/:slug(.:format) article#show
contact GET /contact(.:format) pages#contact
mentionslegales GET /mentionslegales(.:format) pages#mentionslegales
admin_projets GET /admin/projets(.:format) admin/projets#index
POST /admin/projets(.:format) admin/projets#create
new_admin_projet GET /admin/projets/new(.:format) admin/projets#new
edit_admin_projet GET /admin/projets/:id/edit(.:format) admin/projets#edit
admin_projet GET /admin/projets/:id(.:format) admin/projets#show
PATCH /admin/projets/:id(.:format) admin/projets#update
PUT /admin/projets/:id(.:format) admin/projets#update
DELETE /admin/projets/:id(.:format) admin/projets#destroy
admin_articles GET /admin/articles(.:format) admin/articles#index
POST /admin/articles(.:format) admin/articles#create
new_admin_article GET /admin/articles/new(.:format) admin/articles#new
edit_admin_article GET /admin/articles/:id/edit(.:format) admin/articles#edit
admin_article GET /admin/articles/:id(.:format) admin/articles#show
PATCH /admin/articles/:id(.:format) admin/articles#update
PUT /admin/articles/:id(.:format) admin/articles#update
DELETE /admin/articles/:id(.:format) admin/articles#destroy
admin_users GET /admin/users(.:format) admin/users#index
POST /admin/users(.:format) admin/users#create
new_admin_user GET /admin/users/new(.:format) admin/users#new
edit_admin_user GET /admin/users/:id/edit(.:format) admin/users#edit
admin_user GET /admin/users/:id(.:format) admin/users#show
PATCH /admin/users/:id(.:format) admin/users#update
PUT /admin/users/:id(.:format) admin/users#update
DELETE /admin/users/:id(.:format) admin/users#destroy
admin_root GET /admin(.:format) admin/projets#index
I don't understand this error and how to resolve it.
I already read all posts about this error but can't find the solution..
Finally the solution was to with draw theses lines from routes.rb :
if defined?(DashboardManifest)
namespace :admin do
DashboardManifest::DASHBOARDS.each do |dashboard_resource|
resources dashboard_resource
end
root controller: DashboardManifest::ROOT_DASHBOARD, action: :index
end
end

Nested routes work but others don't

Struggling with routing in Rails!
This works: http://127.0.0.1:3000/locations/1/statistics but http://127.0.0.1:3000/locations/ does not work.
My routes look like this:
resources :locations do
resources :statistics
end
I can get only http://127.0.0.1:3000/locations/ working if I just do
resources locations
but then the nested routes don't work!
How can I get both working?
Many thanks.
EDIT rake routes:
location_statistics GET /locations/:location_id/statistics(.:format) statistics#index
POST /locations/:location_id/statistics(.:format) statistics#create
new_location_statistic GET /locations/:location_id/statistics/new(.:format) statistics#new
edit_location_statistic GET /locations/:location_id/statistics/:id/edit(.:format) statistics#edit
location_statistic GET /locations/:location_id/statistics/:id(.:format) statistics#show
PUT /locations/:location_id/statistics/:id(.:format) statistics#update
DELETE /locations/:location_id/statistics/:id(.:format) statistics#destroy
locations GET /locations(.:format) locations#index
POST /locations(.:format) locations#create
new_location GET /locations/new(.:format) locations#new
edit_location GET /locations/:id/edit(.:format) locations#edit
location GET /locations/:id(.:format) locations#show
PUT /locations/:id(.:format) locations#update
DELETE /locations/:id(.:format) locations#destroy
home_index GET /home/index(.:format) home#index
about /about(.:format) home#about
contact /contact(.:format) home#contact
root / home#index
EDIT 2 routes file
match '/about/' => 'home#about'
match '/contact/' => 'home#contact'
resources :locations do
resources :statistics
end
get "home/index"
EDIT 3
My actual error:
Routing Error
No route matches {:controller=>"statistics", :location_id=>nil}
when I go to http://127.0.0.1:3000/locations/
You should either use
=link_to "Locations", locations_path
or
# get sure #location is not nil
=link_to "Location Statistics", location_statistics_path(#location)

Changing every route from /product to /eshop in Rails Route 3

in my routes.rb i have foll entries:-
resources :products do
get 'get_products', :on => :collection
get 'show_product_details',:on=>:member
get 'buy_this',:on=>:collection
get 'search_product',:on=>:collection
end
i want to change every /products to /eshop in the url.
i am not sure but can i use :path=>:eshop.Will it will be also applicable to the sub routes as well such as eshop/get_products,eshop/buy_this...etc.
You can modify your routes and run rake routes in the terminal to check the paths.
resources :products, :path => 'eshop', :as => 'eshop' do
get 'get_products', :on => :collection
get 'show_product_details',:on=>:member
get 'buy_this',:on=>:collection
get 'search_product',:on=>:collection
end
will produce these
get_products_eshop_index GET /eshop/get_products(.:format) products#get_products
show_product_details_eshop GET /eshop/:id/show_product_details(.:format) products#show_product_details
buy_this_eshop_index GET /eshop/buy_this(.:format) products#buy_this
search_product_eshop_index GET /eshop/search_product(.:format) products#search_product
eshop_index GET /eshop(.:format) products#index
POST /eshop(.:format) products#create
new_eshop GET /eshop/new(.:format) products#new
edit_eshop GET /eshop/:id/edit(.:format) products#edit
eshop GET /eshop/:id(.:format) products#show
PUT /eshop/:id(.:format) products#update
DELETE /eshop/:id(.:format) products#destroy

Inserting variable into routes - map.resources :posts, :as => X - is this possible?

Ok, so I am working on a blog application of sorts. Thus far, it allows for a user to sign up for their own account, create posts, tags, comments, etc.
I have just implemented the ability to use www.myapp.com/brandon to set #user to find by username and therefore correctly display the users information at each url. So when you go to www.myapp.com/brandon you see all Brandon's posts, tags, and comments associated with those posts, etc. Works great.
I'm implementing this URL mapping through the routes.rb file by adding the following:
map.username_link '/:username', :controller => 'posts', :action => 'index'
And then just setting the #user variable in the PostController and corresponding views to find_by_username. Now the issue is this. Once at www.myapp.com/brandon when you click on a post title, it sends to myapp.com/posts/id without the username in the URL.
How do I tell rails to replace the /posts with /username.
Is it even possible to insert the user_username variable into this code?
map.resources :posts, :as => [what goes here]
You said there's going to be more than just posts on the page? comments and tags too? Sounds like we need some resource aggregation here...
Another concern: what if a user picks the name faq and you want domain.com/faq down the road? You can't possibly know all the URLs you will want in the future. Prefixing paths with /profiles is a great way to build a little "namespace" to prevent this from happening. So...
Why not a ProfilesController?
script/generate controller profiles index show
routes.rb
ActionController::Routing::Routes.draw do |map|
map.resources :profiles, :only => [:index, :show] do |profile|
profile.resources :posts, :only => [:index, :show]
profile.resources :comments, :only => [:index, :show]
profile.resources :tags, :only => [:index, :show]
end
# ...
end
This will give you the following routes
profiles GET /profiles(.:format) {:controller=>"profiles", :action=>"index"}
profile GET /profiles/:id(.:format) {:controller=>"profiles", :action=>"show"}
profile_posts GET /profiles/:profile_id/posts(.:format) {:controller=>"posts", :action=>"index"}
profile_post GET /profiles/:profile_id/posts/:id(.:format) {:controller=>"posts", :action=>"show"}
profile_comments GET /profiles/:profile_id/comments(.:format) {:controller=>"comments", :action=>"index"}
profile_comment GET /profiles/:profile_id/comments/:id(.:format) {:controller=>"comments", :action=>"show"}
profile_tags GET /profiles/:profile_id/tags(.:format) {:controller=>"tags", :action=>"index"}
profile_tag GET /profiles/:profile_id/tags/:id(.:format) {:controller=>"tags", :action=>"show"}
profiles_controller.rb
class ProfilesController < ApplicationController
# show all profiles; profile browser
# /profiles
def index
end
# show one profile
# /profiles/:id
def show
#user = User.find_by_username(params[:id])
end
end
posts_controller.rb (and others)
class PostsController < ApplicationController
before_filter :find_profile, :only => [:index, :show]
# list all posts for this profile
# /profiles/:profile_id/posts
def index
end
# show one post for this profile
# /profiles/:profile_id/posts/:id
def show
end
protected
def find_profile
#user = User.find_by_username(params[:profile_id])
end
end
You should be able to create the link using:
= link_to "User Posts", subdomain_link_url(#user.username, #post)
In your PostController, then, I would use a before_filter to lookup and set the #user variable:
class PostController < ApplicationController
before_filter :find_user
def other_method
# Some code here
end
protected
def find_user
#user = User.find_by_username(params[:username])
end
end
I don't know much about routes and stuff, so forgive me if this doesn't make sense, but doesn't it works for you?
map.resources :posts, :path_prefix => '/:username' do |post|
post.resources :comments
end
I can see here that this will generate the following
posts GET /:username/posts(.:format) {:controller=>"posts", :action=>"index"}
POST /:username/posts(.:format) {:controller=>"posts", :action=>"create"}
new_post GET /:username/posts/new(.:format) {:controller=>"posts", :action=>"new"}
edit_post GET /:username/posts/:id/edit(.:format) {:controller=>"posts", :action=>"edit"}
post GET /:username/posts/:id(.:format) {:controller=>"posts", :action=>"show"}
PUT /:username/posts/:id(.:format) {:controller=>"posts", :action=>"update"}
DELETE /:username/posts/:id(.:format) {:controller=>"posts", :action=>"destroy"}
post_comments GET /:username/posts/:post_id/comments(.:format) {:controller=>"comments", :action=>"index"}
POST /:username/posts/:post_id/comments(.:format) {:controller=>"comments", :action=>"create"}
new_post_comment GET /:username/posts/:post_id/comments/new(.:format) {:controller=>"comments", :action=>"new"}
edit_post_comment GET /:username/posts/:post_id/comments/:id/edit(.:format) {:controller=>"comments", :action=>"edit"}
post_comment GET /:username/posts/:post_id/comments/:id(.:format) {:controller=>"comments", :action=>"show"}
PUT /:username/posts/:post_id/comments/:id(.:format) {:controller=>"comments", :action=>"update"}
DELETE /:username/posts/:post_id/comments/:id(.:format) {:controller=>"comments", :action=>"destroy"}

Shallow nested Rails routing with :as

I want:
Every projectpart to belong to a
project.
Every solution to belong to a
projectart (and to a project through
that projectpart).
Every image to belong to a solution
(and to a project and a projectpart
through that solution.)
Every document to belong to a
solution (and to a project and a
projectpart through that solution.)
Every URL to be as short as simple as
possible.
Every case of "projectpart" to appear
as "part" in every URL. (I couldn't
call the model "part" on Heroku.)
Can anyone tell me why this...
ActionController::Routing::Routes.draw do |map|
map.resources :projects, :shallow => true do |project|
project.resources :projectparts do |part|
part.resources :solutions do |solution|
solution.resources :images
solution.resources :documents
end
end
end
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
...is leaving a whole bunch of cases of "projectpart" in my URLs...
steven-nobles-imac-200:drominay steven$ rake routes
(in /Users/steven/Drominay)
projects GET /projects {:controller=>"projects", :action=>"index"}
formatted_projects GET /projects.:format {:controller=>"projects", :action=>"index"}
POST /projects {:controller=>"projects", :action=>"create"}
POST /projects.:format {:controller=>"projects", :action=>"create"}
new_project GET /projects/new {:controller=>"projects", :action=>"new"}
formatted_new_project GET /projects/new.:format {:controller=>"projects", :action=>"new"}
edit_project GET /projects/:id/edit {:controller=>"projects", :action=>"edit"}
formatted_edit_project GET /projects/:id/edit.:format {:controller=>"projects", :action=>"edit"}
project GET /projects/:id {:controller=>"projects", :action=>"show"}
formatted_project GET /projects/:id.:format {:controller=>"projects", :action=>"show"}
PUT /projects/:id {:controller=>"projects", :action=>"update"}
PUT /projects/:id.:format {:controller=>"projects", :action=>"update"}
DELETE /projects/:id {:controller=>"projects", :action=>"destroy"}
DELETE /projects/:id.:format {:controller=>"projects", :action=>"destroy"}
project_projectparts GET /projects/:project_id/projectparts {:controller=>"projectparts", :action=>"index"}
formatted_project_projectparts GET /projects/:project_id/projectparts.:format {:controller=>"projectparts", :action=>"index"}
POST /projects/:project_id/projectparts {:controller=>"projectparts", :action=>"create"}
POST /projects/:project_id/projectparts.:format {:controller=>"projectparts", :action=>"create"}
new_project_projectpart GET /projects/:project_id/projectparts/new {:controller=>"projectparts", :action=>"new"}
formatted_new_project_projectpart GET /projects/:project_id/projectparts/new.:format {:controller=>"projectparts", :action=>"new"}
edit_projectpart GET /projectparts/:id/edit {:controller=>"projectparts", :action=>"edit"}
formatted_edit_projectpart GET /projectparts/:id/edit.:format {:controller=>"projectparts", :action=>"edit"}
projectpart GET /projectparts/:id {:controller=>"projectparts", :action=>"show"}
formatted_projectpart GET /projectparts/:id.:format {:controller=>"projectparts", :action=>"show"}
PUT /projectparts/:id {:controller=>"projectparts", :action=>"update"}
PUT /projectparts/:id.:format {:controller=>"projectparts", :action=>"update"}
DELETE /projectparts/:id {:controller=>"projectparts", :action=>"destroy"}
DELETE /projectparts/:id.:format {:controller=>"projectparts", :action=>"destroy"}
projectpart_solutions GET /projectparts/:projectpart_id/solutions {:controller=>"solutions", :action=>"index"}
formatted_projectpart_solutions GET /projectparts/:projectpart_id/solutions.:format {:controller=>"solutions", :action=>"index"}
POST /projectparts/:projectpart_id/solutions {:controller=>"solutions", :action=>"create"}
POST /projectparts/:projectpart_id/solutions.:format {:controller=>"solutions", :action=>"create"}
new_projectpart_solution GET /projectparts/:projectpart_id/solutions/new {:controller=>"solutions", :action=>"new"}
formatted_new_projectpart_solution GET /projectparts/:projectpart_id/solutions/new.:format {:controller=>"solutions", :action=>"new"}
edit_solution GET /solutions/:id/edit {:controller=>"solutions", :action=>"edit"}
formatted_edit_solution GET /solutions/:id/edit.:format {:controller=>"solutions", :action=>"edit"}
solution GET /solutions/:id {:controller=>"solutions", :action=>"show"}
formatted_solution GET /solutions/:id.:format {:controller=>"solutions", :action=>"show"}
PUT /solutions/:id {:controller=>"solutions", :action=>"update"}
PUT /solutions/:id.:format {:controller=>"solutions", :action=>"update"}
DELETE /solutions/:id {:controller=>"solutions", :action=>"destroy"}
DELETE /solutions/:id.:format {:controller=>"solutions", :action=>"destroy"}
solution_images GET /solutions/:solution_id/images {:controller=>"images", :action=>"index"}
formatted_solution_images GET /solutions/:solution_id/images.:format {:controller=>"images", :action=>"index"}
POST /solutions/:solution_id/images {:controller=>"images", :action=>"create"}
POST /solutions/:solution_id/images.:format {:controller=>"images", :action=>"create"}
new_solution_image GET /solutions/:solution_id/images/new {:controller=>"images", :action=>"new"}
formatted_new_solution_image GET /solutions/:solution_id/images/new.:format {:controller=>"images", :action=>"new"}
edit_image GET /images/:id/edit {:controller=>"images", :action=>"edit"}
formatted_edit_image GET /images/:id/edit.:format {:controller=>"images", :action=>"edit"}
image GET /images/:id {:controller=>"images", :action=>"show"}
formatted_image GET /images/:id.:format {:controller=>"images", :action=>"show"}
PUT /images/:id {:controller=>"images", :action=>"update"}
PUT /images/:id.:format {:controller=>"images", :action=>"update"}
DELETE /images/:id {:controller=>"images", :action=>"destroy"}
DELETE /images/:id.:format {:controller=>"images", :action=>"destroy"}
solution_documents GET /solutions/:solution_id/documents {:controller=>"documents", :action=>"index"}
formatted_solution_documents GET /solutions/:solution_id/documents.:format {:controller=>"documents", :action=>"index"}
POST /solutions/:solution_id/documents {:controller=>"documents", :action=>"create"}
POST /solutions/:solution_id/documents.:format {:controller=>"documents", :action=>"create"}
new_solution_document GET /solutions/:solution_id/documents/new {:controller=>"documents", :action=>"new"}
formatted_new_solution_document GET /solutions/:solution_id/documents/new.:format {:controller=>"documents", :action=>"new"}
edit_document GET /documents/:id/edit {:controller=>"documents", :action=>"edit"}
formatted_edit_document GET /documents/:id/edit.:format {:controller=>"documents", :action=>"edit"}
document GET /documents/:id {:controller=>"documents", :action=>"show"}
formatted_document GET /documents/:id.:format {:controller=>"documents", :action=>"show"}
PUT /documents/:id {:controller=>"documents", :action=>"update"}
PUT /documents/:id.:format {:controller=>"documents", :action=>"update"}
DELETE /documents/:id {:controller=>"documents", :action=>"destroy"}
DELETE /documents/:id.:format {:controller=>"documents", :action=>"destroy"}
/:controller/:action/:id
/:controller/:action/:id.:format
...and yet is not generating basic URL helpers, like this?
undefined method `project_projectpart_path' for #<ActionView::Base:0x3438ffc> (ActionView::TemplateError)
BTW, everything except changing "projectpart" to "part" in every URL was working fine with this more verbose syntax:
ActionController::Routing::Routes.draw do |map|
map.resources :projects, :has_many => :projectparts
map.resources :projects, :has_many => :solutions
map.resources :projects, :has_many => :images
map.resources :projects, :has_many => :documents
map.resources :projectparts, :has_many => :solutions
map.resources :projectparts, :has_many => :images
map.resources :projectparts, :has_many => :documents
map.resources :solutions, :has_many => :images
map.resources :solutions, :has_many => :documents
map.resources :images
map.resources :documents
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
The answer to your question about route generation is :shallow => true
Providing the shallow option will create longer routes for collection methods of the inner resource(s), while providing shallow routes for member methods of those nested resource(s). Take a closer look at the routes created and you'll see this.
Essentially routes that need an id (edit,update,show,destroy) will the short one containing only the inner most resource . Routes that don't need an id(index,new) will be slightly longer, containing the immediate parent.
You don't need project_projectpart_path because the project in project_projectpart can be implied from the project part. Instead just use projectpart_path for existing project parts.
Your second example provides those missing routes because it never gives the shallow option.
Changing 'projectpart' to 'part' in urls is as simple as adding the :as option in it's definition. Sorry it won't work with the shorthand :has_many version
map.resources :projects, :shallow => true do |project|
project.resources :projectparts, :as => "part" do |part|
part.resources :solutions do |solution|
solution.resources :images
solution.resources :documents
end
end
end

Resources