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.
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
I accidentally generated a singular controller comm_log without 's'. 's' was added to controller name, helpers, views, specs and routes. The customer_comm_log_path(f.customer_id, f.id) seems not right.
The relation is a comm log belongs to a customer and a customer has many comm logs.
resources :customers do
resources :comm_logs
end
The output of rake routes (related) is:
comm_logs_index GET /comm_logs/index(.:format) {:controller=>"comm_logs", :action=>"index"}
comm_logs_new GET /comm_logs/new(.:format) {:controller=>"comm_logs", :action=>"new"}
comm_logs_create GET /comm_logs/create(.:format) {:controller=>"comm_logs", :action=>"create"}
comm_logs_show GET /comm_logs/show(.:format) {:controller=>"comm_logs", :action=>"show"}
comm_logs_destroy GET /comm_logs/destroy(.:format) {:controller=>"comm_logs", :action=>"destroy"}
customer_comm_logs GET /customers/:customer_id/comm_logs(.:format) {:action=>"index", :controller=>"comm_logs"}
POST /customers/:customer_id/comm_logs(.:format) {:action=>"create", :controller=>"comm_logs"}
new_customer_comm_log GET /customers/:customer_id/comm_logs/new(.:format) {:action=>"new", :controller=>"comm_logs"}
edit_customer_comm_log GET /customers/:customer_id/comm_logs/:id/edit(.:format) {:action=>"edit", :controller=>"comm_logs"}
customer_comm_log GET /customers/:customer_id/comm_logs/:id(.:format) {:action=>"show", :controller=>"comm_logs"}
PUT /customers/:customer_id/comm_logs/:id(.:format) {:action=>"update", :controller=>"comm_logs"}
DELETE /customers/:customer_id/comm_logs/:id(.:format) {:action=>"destroy", :controller=>"comm_logs"}
Is there a way to fix the singular controller? Thanks.
Try script/destroy your_controller_name using the same controller name used to create it.
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.
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
Is it possible to have a variable namespace? I have restful resources like the following:
resources :articles
resources :persons
But I need to scope these inside a variable namespace, such that it responds to URLs of the form:
':edition/:controller/:action/:id'
for example:
/foobar/article/edit/123 or /bazbam/person/edit/345
for each of the resources. Is this possible with the resources method, or must I hand-craft these? I will not know the possible values for :edition ahead of time; these get looked up in a before_filter in my ApplicationController.
Is this all I need to do?
scope ':edition' do
resources :articles
resources :matches
resources :teams
end
UPDATE: When using the scope directive above, I get routes like I want:
articles GET /:edition/articles(.:format) {:action=>"index", :controller=>"articles"}
POST /:edition/articles(.:format) {:action=>"create", :controller=>"articles"}
new_article GET /:edition/articles/new(.:format) {:action=>"new", :controller=>"articles"}
edit_article GET /:edition/articles/:id/edit(.:format) {:action=>"edit", :controller=>"articles"}
article GET /:edition/articles/:id(.:format) {:action=>"show", :controller=>"articles"}
PUT /:edition/articles/:id(.:format) {:action=>"update", :controller=>"articles"}
DELETE /:edition/articles/:id(.:format) {:action=>"destroy", :controller=>"articles"}
matches GET /:edition/matches(.:format) {:action=>"index", :controller=>"matches"}
POST /:edition/matches(.:format) {:action=>"create", :controller=>"matches"}
new_match GET /:edition/matches/new(.:format) {:action=>"new", :controller=>"matches"}
edit_match GET /:edition/matches/:id/edit(.:format) {:action=>"edit", :controller=>"matches"}
match GET /:edition/matches/:id(.:format) {:action=>"show", :controller=>"matches"}
PUT /:edition/matches/:id(.:format) {:action=>"update", :controller=>"matches"}
DELETE /:edition/matches/:id(.:format) {:action=>"destroy", :controller=>"matches"}
teams GET /:edition/teams(.:format) {:action=>"index", :controller=>"teams"}
POST /:edition/teams(.:format) {:action=>"create", :controller=>"teams"}
new_team GET /:edition/teams/new(.:format) {:action=>"new", :controller=>"teams"}
edit_team GET /:edition/teams/:id/edit(.:format) {:action=>"edit", :controller=>"teams"}
team GET /:edition/teams/:id(.:format) {:action=>"show", :controller=>"teams"}
PUT /:edition/teams/:id(.:format) {:action=>"update", :controller=>"teams"}
DELETE /:edition/teams/:id(.:format) {:action=>"destroy", :controller=>"teams"}
I'm now able to reference :edition in my ApplicationController:
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :authenticate_user!
before_filter :get_edition
def get_edition
#edition = Edition.first(:conditions => { :FriendlyName => params[:edition] } )
end
end
Now I just want to make sure this is the best way to accomplish this.
Actually, you can just do the following :
my_var = "persons"
resources my_var.to_sym
The to_sym method on a string changes it to a symbol
If you don't know the possible values for edition - then you can't use namespaces which seem like they might have solved this issue.
That said, I'd just handcraft them - your case here seems like the ideal case foregoing resources and going straight to a handcrafted path.