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.
Related
I'm using slugs for IDs, so wanting URLs like /songs/radiohead/karma-police instead of /artists/radiohead/songs/karma-police.
Slugs can be achieved with:
def to_param
slug
end
But how is there any way to drop the model name - "songs" - from the standard RESTful URL?
You can override the path segment by passing the :path option to your resources call.
resources :songs, path: "songs/:artist_id"
this will generate these routes
songs GET /songs/:artist_id(.:format) {:action=>"index", :controller=>"songs"}
POST /songs/:artist_id(.:format) {:action=>"create", :controller=>"songs"}
new_song GET /songs/:artist_id/new(.:format) {:action=>"new", :controller=>"songs"}
edit_song GET /songs/:artist_id/:id/edit(.:format) {:action=>"edit", :controller=>"songs"}
song GET /songs/:artist_id/:id(.:format) {:action=>"show", :controller=>"songs"}
PUT /songs/:artist_id/:id(.:format) {:action=>"update", :controller=>"songs"}
DELETE /songs/:artist_id/:id(.:format) {:action=>"destroy", :controller=>"songs"}
Put this in your routes.rb and it should work.
match 'artists/:artist_id/:id' => 'songs#show', :as => 'artist_song'
Make sure if you do the :as that the other routes don't take precedence over this one.
Then check out this Routing match reference
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 want to have a namespaced controller called 'portal'.
in this will be nested resources such as companies and products.
I'd like routes like :
/portal/:company_id/product/:id to work
I can get
/portal/company/:company_id/product/:id to work but would like to eliminate the 'company' in the url
Hope that is clear. Please keep in mind that I need the namespaced module portal to exist.
I think you could use scope to achieve what you want. Perhaps like this:
namespace "portal" do
scope ":company_id" do
resources :products
end
end
That will generate the following routes:
portal_products GET /portal/:company_id/products(.:format) {:action=>"index", :controller=>"portal/products"}
POST /portal/:company_id/products(.:format) {:action=>"create", :controller=>"portal/products"}
new_portal_product GET /portal/:company_id/products/new(.:format) {:action=>"new", :controller=>"portal/products"}
edit_portal_product GET /portal/:company_id/products/:id/edit(.:format) {:action=>"edit", :controller=>"portal/products"}
portal_product GET /portal/:company_id/products/:id(.:format) {:action=>"show", :controller=>"portal/products"}
PUT /portal/:company_id/products/:id(.:format) {:action=>"update", :controller=>"portal/products"}
DELETE /portal/:company_id/products/:id(.:format) {:action=>"destroy", :controller=>"portal/products"}
Edit: Accidentally used resource instead of resources. Fixed now.
You can customize the routes to nearly whatever you want if you spell them out directly, like this:
match '/portal/:company_id/product/:id', :to => 'companies_products#show'
The :to part specifies the controller and action to use, something that should match what you have in your routes now. If you're not sure what that is, rake routes will tell you its specific interpretation.
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.
I want to create a route in my rails application along the lines of
/panda/blog
/tiger/blog
/dog/blog
where panda, tiger, and dog are all permalinks (for an animal class)
The normal way of doing this
map.resources :animals do |animal|
animal.resource :blog
end
would create routes along the lines of
/animals/panda/blog
/animals/tiger/blog
/animals/dog/blog
But i do not want the first segment, as it will always be the same.
I know I could do this by manual routing, but I want to know how to do using rails resources, as having animals and blogs is a requirement for me.
In rails 3.x, you can add path => "" to any resource or resources call to remove the first segment from the generated path.
resources :animals, :path => ""
$ rake routes
animals GET / {:action=>"index", :controller=>"animals"}
POST / {:action=>"create", :controller=>"animals"}
new_animal GET /new(.:format) {:action=>"new", :controller=>"animals"}
edit_animal GET /:id/edit(.:format) {:action=>"edit", :controller=>"animals"}
animal GET /:id(.:format) {:action=>"show", :controller=>"animals"}
PUT /:id(.:format) {:action=>"update", :controller=>"animals"}
DELETE /:id(.:format) {:action=>"destroy", :controller=>"animals"}
You can use this plugin:
http://github.com/caring/default_routing/tree/master