I have a simple rails application in which I'm trying to add a very simple type of record ("client_types") to a database.
I have a route in routes.rb which reads:
resources :client_types
And as I understand it, should be a proxy to all of the conventional routes for my client_types resource.
So when I browse to the following URL http://localhost:3000/client_types/new, I get the following routing error at runtime:
No route matches {:action=>"show", :controller=>"client_types"}
Notice the action in question here is show, not new (and I have a method for both of these in my controller).
So... I added the following route below the resources route above, and viola, it works:
match 'client_types/new' => 'client_types#new', :as => :client_type
So what am I missing? My assumption was that resources :client_types in my routing file would have added a route matching the one I explicitly added later.
rake routes reveals the following:
client_types GET /client_types(.:format) {:action=>"index", :controller=>"client_types"}
POST /client_types(.:format) {:action=>"create", :controller=>"client_types"}
new_client_type GET /client_types/new(.:format) {:action=>"new", :controller=>"client_types"}
edit_client_type GET /client_types/:id/edit(.:format) {:action=>"edit", :controller=>"client_types"}
client_type GET /client_types/:id(.:format) {:action=>"show", :controller=>"client_types"}
PUT /client_types/:id(.:format) {:action=>"update", :controller=>"client_types"}
DELETE /client_types/:id(.:format) {:action=>"destroy", :controller=>"client_types"}
client_type /client_types/new(.:format) {:controller=>"client_types", :action=>"new"}
This is now working. I had an issue in one of my views and this error message was a red herring.
Related
In my routes.rb there is
resources :clients_assessments do
member do
get :medical_edit
get :mental_health_edit
get :personal_edit
end
collection do
end
end
Which gives routes
medical_edit_clients_assessment GET /clients_assessments/:id/medical_edit(.:format) {:action=>"medical_edit", :controller=>"clients_assessments"}
mental_health_edit_clients_assessment GET /clients_assessments/:id/mental_health_edit(.:format) {:action=>"mental_health_edit", :controller=>"clients_assessments"}
personal_edit_clients_assessment GET /clients_assessments/:id/personal_edit(.:format) {:action=>"personal_edit", :controller=>"clients_assessments"}
clients_assessments GET /clients_assessments(.:format) {:action=>"index", :controller=>"clients_assessments"}
POST /clients_assessments(.:format) {:action=>"create", :controller=>"clients_assessments"}
new_clients_assessment GET /clients_assessments/new(.:format) {:action=>"new", :controller=>"clients_assessments"}
edit_clients_assessment GET /clients_assessments/:id/edit(.:format) {:action=>"edit", :controller=>"clients_assessments"}
clients_assessment GET /clients_assessments/:id(.:format) {:action=>"show", :controller=>"clients_assessments"}
PUT /clients_assessments/:id(.:format) {:action=>"update", :controller=>"clients_assessments"}
DELETE /clients_assessments/:id(.:format) {:action=>"destroy", :controller=>"clients_assessments"}
When I try to use them in a helper file like
route = medical_edit_clients_assessment_path(id)
An error message is generated
No route matches {:action=>"medical_edit", :controller=>"clients_assessments"}
I have to change it to
route = "/clients_assessment/#{able_id}/medical_edit"
to get around the error message. What is funny though that a route like
route = (able_id.nil? ? new_client_path : edit_client_path(able_id))
works just fine.
I know someone one will ask for it so here is the entire routes.rb and the result of the rake routes
https://gist.github.com/3074287
Thanks, Russ
This might happened because id is nil
in my routes.rb file there's just two lines:
match 'movies/orderby/:field' => 'movies#orderby'
and
resources :movies
However, when I run rake routes on my project, I get a funny output, look:
/movies/orderby/:field(.:format) {:controller=>"movies", :action=>"orderby"}
movies GET /movies(.:format) {:action=>"index", :controller=>"movies"}
POST /movies(.:format) {:action=>"create",:controller=>"movies"}
new_movie GET /movies/new(.:format) {:action=>"new", :controller=>"movies"}
edit_movie GET /movies/:id/edit(.:format) {:action=>"edit", :controller=>"movies"}
movie GET /movies/:id(.:format) {:action=>"show", :controller=>"movies"}
PUT /movies/:id(.:format) {:action=>"update",:controller=>"movies"}
DELETE /movies/:id(.:format) {:action=>"destroy", :controller=>"movies"}
You see how the route I've hand-coded is different from the others? (it's the one at the top) Also, Rails has not created a url helper for me....
I get the following error message all the time:
undefined method `movies_orderby' for
...Any ideas????
EDIT: the route works (i.e. if i type a matching URL, it get routed correctly) but I got no url helper method to put in my views!!
When you define routes, the method match will not generate url helpers unless you specify what the name of the helpers should be. So I would recommend that you define it as following:
match 'movies/orderby/:field' => 'movies#orderby', :as => :movies_orderby
resources :movies
When you define the name of the route with :as then you will be able to use it in your views like this if you for example would like to order by title
<%= movies_orderby_path("title") %>
And as a side note, you had correctly defined the match route before the resources route. The other way around could have caused problems.
I know this is a newbie question, but I haven't seen an explanation and I'd like one.
What exactly does it mean when Rails issues a routing error like this:
Routing Error
No route matches {:action=>"show", :controller=>"library_imports", :library_id=>#<Library id: 1, ...
What puzzles me is that the message itself shows that my request is being routed to the show action of the library_imports controller. How does that happen if the request URL didn't match any routes?
For the sake of completeness, the URL I'm hitting that results in this error is:
http://localhost:3000/libraries/2/library_imports
which should map to the "index" action, not "show".
The relevant part of config/routes.rb is:
Import::Application.routes.draw do
resources :libraries do
resources :library_imports
end
And the pertinent portion of rake routes output is:
library_library_imports GET /libraries/:library_id/library_imports(.:format) {:action=>"index", :controller=>"library_imports"}
POST /libraries/:library_id/library_imports(.:format) {:action=>"create", :controller=>"library_imports"}
new_library_library_import GET /libraries/:library_id/library_imports/new(.:format) {:action=>"new", :controller=>"library_imports"}
edit_library_library_import GET /libraries/:library_id/library_imports/:id/edit(.:format) {:action=>"edit", :controller=>"library_imports"}
library_library_import GET /libraries/:library_id/library_imports/:id(.:format) {:action=>"show", :controller=>"library_imports"}
PUT /libraries/:library_id/library_imports/:id(.:format) {:action=>"update", :controller=>"library_imports"}
DELETE /libraries/:library_id/library_imports/:id(.:format) {:action=>"destroy", :controller=>"library_imports"}
The error message is misleading for what it doesn't say, rather than what it says. There is indeed no route that can be constructed from :controller => :library_imports, :action => :show, :library_id => #library, because that hash omits the required :id parameter.
It might be less confusing if there were some hint, like (did you leave out a required parameter?).
It's also useful to note that this error is generated (I believe) in the url_for helper, not in the dispatcher. So the request is not actually being routed at all.
I'm confused by my rake routes output. For an example (trimmed):
profil GET /profil/:id(.:format) {:action=>"show", :controller=>"profil"}
PUT /profil/:id(.:format) {:action=>"update", :controller=>"profil"}
login GET /login(.:format) {:action=>"new", :controller=>"sessions"}
POST /login(.:format) {:action=>"create", :controller=>"sessions"}
logout GET /logout(.:format) {:action=>"destroy", :controller=>"sessions"}
I've always thought:
Line 2: Route can be accessed using profil_path with PUT method.
Line 4: Route can be accessed using login_path with POST method.
Conclusion: Lines with the first column empty (line 2 and 4) would follow the one above it.
However, I've been experimenting with adding parameter to the url. So, I added these codes in my routes.rb:
namespace :admin do
resources :pengguna_bulk, :only => [:new, :create]
resources :pengguna do
collection do
get 'index/:page', :action => :index
end
end
end
New rake routes output (trimmed):
admin_pengguna_bulk_index POST /admin/pengguna_bulk(.:format) {:action=>"create", :controller=>"admin/pengguna_bulk"}
new_admin_pengguna_bulk GET /admin/pengguna_bulk/new(.:format) {:action=>"new", :controller=>"admin/pengguna_bulk"}
GET /admin/pengguna/index/:page(.:format) {:action=>"index", :controller=>"admin/pengguna"}
admin_pengguna_index GET /admin/pengguna(.:format) {:action=>"index", :controller=>"admin/pengguna"}
POST /admin/pengguna(.:format) {:action=>"create", :controller=>"admin/pengguna"}
new_admin_pengguna GET /admin/pengguna/new(.:format) {:action=>"new", :controller=>"admin/pengguna"}
edit_admin_pengguna GET /admin/pengguna/:id/edit(.:format) {:action=>"edit", :controller=>"admin/pengguna"}
admin_pengguna GET /admin/pengguna/:id(.:format) {:action=>"show", :controller=>"admin/pengguna"}
PUT /admin/pengguna/:id(.:format) {:action=>"update", :controller=>"admin/pengguna"}
DELETE /admin/pengguna/:id(.:format) {:action=>"destroy", :controller=>"admin/pengguna"}
My question is, why is the 3rd route looks like it's under the 2nd route? Is it empty because Rails do not know what to name it and I'd have to use get 'index/:page', :action => :index, :as => :page to name it?
So, this means, route with an empty first column doesn't always follow the above path?
I've always thought:
Line 2: Route can be accessed using profil_path with PUT method.
Line 4: Route can be accessed using login_path with POST method.
Conclusion: Lines with the first column empty (line 2 and 4) would
follow the one above it.
Everything's correct except the conclusion. profil_path expands to /profil/:id(.:format). If it is called with method GET it responds to your first route, if its called with method PUT it responds to your second route.
But same doesn't hold true for second set of routes. You don't have any named helper for /admin/pengguna/index/:page(.:format). If you want a named helper, you should define the route like:
get 'index/:page', :action => :index, :as => :what_ever_named_helper_you_want
I have the following in my routing config:
resources :users do
resources :apps, :controller => :user_apps
end
rake routes includes the following:
user_apps GET /users/:user_id/apps(.:format) {:action=>"index", :controller=>"user_apps"}
user_apps POST /users/:user_id/apps(.:format) {:action=>"create", :controller=>"user_apps"}
new_user_app GET /users/:user_id/apps/new(.:format) {:action=>"new", :controller=>"user_apps"}
edit_user_app GET /users/:user_id/apps/:id/edit(.:format) {:action=>"edit", :controller=>"user_apps"}
user_app GET /users/:user_id/apps/:id(.:format) {:action=>"show", :controller=>"user_apps"}
user_app PUT /users/:user_id/apps/:id(.:format) {:action=>"update", :controller=>"user_apps"}
user_app DELETE /users/:user_id/apps/:id(.:format) {:action=>"destroy", :controller=>"user_apps"}
However, when I try to access eg user_apps_path(1,2) I get /users/1/apps.2 rather than /users/1/apps/2.
Where am I going wrong?
I'm using rails 3.
The correct route is user_app_path(1,2) The pluralized version goes to the index action, making the second argument the format / extension of the request.