Route missing on nested route - ruby-on-rails

Not sure why this isn't working. It was working perfectly and then I restarted the server. I am getting a no route error:
No route matches {:action=>"new", :controller=>"issues"}
Here is the helper i am trying to call:
<%= link_to "Add Issue", new_app_issue_path %>
and my routes file:
resources :apps do
resources :issues
end
Why would it break all of a sudden? And I obviously have the route:
app_issues GET /apps/:app_id/issues(.:format) {:action=>"index", :controller=>"issues"}
POST /apps/:app_id/issues(.:format) {:action=>"create", :controller=>"issues"}
new_app_issue GET /apps/:app_id/issues/new(.:format) {:action=>"new", :controller=>"issues"}

You need to specify what app this is for:
new_app_issue_path(an_app_object_or_an_app_objects_id)
Rails would only "guess" this if params[:app_id] was available, which doesn't appear to be the case here.

Related

Confusing routing error

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.

Routing resource does not handle "new" as expected

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.

Rails member routing, should be easy?

For this route:
resources :projects do
member do
resources :stakeholders
end
end
The generated routes are:
projects_stakeholders GET /projects/projects/:id/stakeholders(.:format) {:action=>"index", :controller=>"projects/stakeholders"}
POST /projects/projects/:id/stakeholders(.:format) {:action=>"create", :controller=>"projects/stakeholders"}
new_projects_stakeholder GET /projects/projects/:id/stakeholders/new(.:format) {:action=>"new", :controller=>"projects/stakeholders"}
edit_projects_stakeholder GET /projects/projects/:id/stakeholders/:id/edit(.:format) {:action=>"edit", :controller=>"projects/stakeholders"}
projects_stakeholder GET /projects/projects/:id/stakeholders/:id(.:format) {:action=>"show", :controller=>"projects/stakeholders"}
As these routes have two times a :id parameter, if I have, for instance the URL 'projects/4/stakeholders/11'
In my log file I see this:
Parameters: {"id"=>"11"}
How can I then access my project_id from inside my controller?
Thanks!!!
You don't need the member do block around it. Just do this, and you should start seeing a project_id in your params:
resources :projects do
resources :stakeholders
end

Rails 3 restful route problem

The following link works in my app:
<%= link_to "invitation", :controller => :invitations, :action => :index %>
To follow restful conventions i changed the link to:
<%= link_to "invitation", index_invitation_path %>
The error that i get is:
undefined local variable or method `index_invitation_path'
Rake routes yields:
invitations GET /invitations(.:format) {:controller=>"invitations", :action=>"index"}
The page name is index.html.erb. The model is invitation.rb. The controller is invitation_controller.rb. Routes has resources :invitations. What am i missing?
Thanks!
Assuming you have the routing correct:
resources :invitations
Then the correct helper for the index action (with the url /invitations.html) is
invitations_path
You can see more information by running rake routes. It will display text like the following:
lists GET /lists(.:format)
{:action=>"index", :controller=>"lists"}
POST /lists(.:format)
{:action=>"create", :controller=>"lists"}
new_list GET /lists/new(.:format)
{:action=>"new", :controller=>"lists"}
edit_list GET /lists/:id/edit(.:format)
{:action=>"edit", :controller=>"lists"}
list GET /lists/:id(.:format)
{:action=>"show", :controller=>"lists"}
PUT /lists/:id(.:format)
{:action=>"update", :controller=>"lists"}
DELETE /lists/:id(.:format)
{:action=>"destroy", :controller=>"lists"}
root /(.:format)
{:controller=>"lists", :action=>"index"}
The above was from a route of my own (for a model called List). The route helper method is shown immediately before the HTTP method. You have to remember to append the _path to each helper method. For example the helper methods I could use are:
list_path(list)
edit_list_path(list)
new_list_path
lists_path
You'll need a route in your routes.rb file that defines a mapping to the invitations controller and the index action.
Typically this is created with a resources call
resources :invitations
Which creates several default routes, which you can see by running rake routes.
For single resources, you can also define it using a match call
match "invitations/:id" => "invitations#index", :as => index_invitation
The rails site has a great resource on routing that provides all the details: Routing from the Outside In
Update: Based on your updated question, your route includes an invitaions (notice the trailing 's') route - nothing with index or invitation. The index_ prefix is generated by the resources call when it creates the default routes for :invitations.
It looks like you've defined a custom get mapping for an invitation. While this may technically work, if you're aim is to support restful routes, use the resources method. And have a read of the Routing guide from rails it's very easy to follow and quite detailed.
type rake routes in your console and look at listing of available routes. Seems to be there is no such route index_invitation_path? maybe it named differently
I think you need "invitations_controller.rb" to contain InvitationsController. Plural.

Problem with rails routing

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.

Resources