I have the following Nested Resource in my routes.rb file
resource :issuer do
resources :certificates
end
My Models:
class Issuer < ActiveRecord::Base
has_many :certificates
end
class Certificate < ActiveRecord::Base
belongs_to :issuer
end
I get the following error when i visit: /issuer/2/certificates
No route matches {:action=>"edit", :controller=>"certificates"}
Any help would be appreciated.. Thanks in advance!
EDIT : MY rake routes
issuer_certificates GET /issuer/:issuer_id/certificates(.:format) {:action=>"index", :controller=>"certificates"}
POST /issuer/:issuer_id/certificates(.:format) {:action=>"create", :controller=>"certificates"}
new_issuer_certificate GET /issuer/:issuer_id/certificates/new(.:format) {:action=>"new", :controller=>"certificates"}
edit_issuer_certificate GET /issuer/:issuer_id/certificates/:id/edit(.:format) {:action=>"edit", :controller=>"certificates"}
issuer_certificate GET /issuer/:issuer_id/certificates/:id(.:format) {:action=>"show", :controller=>"certificates"}
PUT /issuer/:issuer_id/certificates/:id(.:format) {:action=>"update", :controller=>"certificates"}
DELETE /issuer/:issuer_id/certificates/:id(.:format) {:action=>"destroy", :controller=>"certificates"}
You should use resources (plural) instead of resource in refer to :issuer. So please try this:
resources :issuers do
resources :certificates
end
and issuers/2/certificates will work.
In your routes.rb file you are defining issuer as a singular resource
resource :issuer do # <----- using resource instead of resources
resources :certificates
end
With singular resources, the routes you get won't allow an :id to be matched, so in your example,
issuer/2/certificates
it's the "2" that's giving you grief. You should either get rid of it or define :issuer as a standard (non-singular) resource in your routes.rb file.
You don't have PUT defined for /issuer/2/certificates. instead you have it defined for /issuer/2/certificates/123. That's why you get that error.
If you are trying to edit the certificate then make sure you generate a correct link and include certificate_id.
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
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
I have a has_one relation:
# supplier.rb
has_one :presentation
...
# presentation.rb
belongs_to :supplier
...
and the folowing nested routes for them:
# routes.rb
resources :suppliers do
resource :presentation
end
running rake routesgives:
supplier_presentation POST ... {:action=>"create", :controller=>"presentations"}
new_supplier_presentation GET ... {:action=>"new", :controller=>"presentations"}
edit_supplier_presentation GET ... {:action=>"edit", :controller=>"presentations"}
GET ... {:action=>"show", :controller=>"presentations"}
PUT ... {:action=>"update", :controller=>"presentations"}
DELETE ... {:action=>"destroy", :controller=>"presentations"}
Why no name_helper for the show action?
I can override the problem doing something like:
resources :suppliers do
resource :presentation, :except => :show do
get "" => "presentations#show", as: "presentation"
end
end
giving the route:
presentation_supplier_presentation GET ... {:controller=>"presentations", :action=>"show"}
but we all now that's not the right way to deal with it..
ANY SUGGESTIONS?
--
(edited)
supplier_presentation_path(#supplier)
does work, but why?... It doesn't appear when rake routes is executed on my shell...
I dont really know why it's not displayed when you do rake routes but did you try in your code to do supplier_presentation_path(#supplier)? It should work based on your routes.
Never the less it should work for you. Try this:
link_to "Presentation", [#suplier, #presentation]
or
link_to "Presentation", suplier_presentation_path(#suplier, #presentation)
I can't understand what the difference is between a namespace and a scope in the routing of ruby-on-rails 3.
Could someone please explain?
namespace "admin" do
resources :posts, :comments
end
scope :module => "admin" do
resources :posts, :comments
end
The difference lies in the paths generated.
The paths are admin_posts_path and admin_comments_path for the namespace, while they are just posts_path and comments_path for the scope.
You can get the same result as a namespace by passing the :name_prefix option to scope.
examples always help me, so here is an example:
namespace :blog do
resources :contexts
end
will give us the following routes:
blog_contexts GET /blog/contexts(.:format) {:action=>"index", :controller=>"blog/contexts"}
POST /blog/contexts(.:format) {:action=>"create", :controller=>"blog/contexts"}
new_blog_context GET /blog/contexts/new(.:format) {:action=>"new", :controller=>"blog/contexts"}
edit_blog_context GET /blog/contexts/:id/edit(.:format) {:action=>"edit", :controller=>"blog/contexts"}
blog_context GET /blog/contexts/:id(.:format) {:action=>"show", :controller=>"blog/contexts"}
PUT /blog/contexts/:id(.:format) {:action=>"update", :controller=>"blog/contexts"}
DELETE /blog/contexts/:id(.:format) {:action=>"destroy", :controller=>"blog/contexts"}
Using scope...
scope :module => 'blog' do
resources :contexts
end
Will give us:
contexts GET /contexts(.:format) {:action=>"index", :controller=>"blog/contexts"}
POST /contexts(.:format) {:action=>"create", :controller=>"blog/contexts"}
new_context GET /contexts/new(.:format) {:action=>"new", :controller=>"blog/contexts"}
edit_context GET /contexts/:id/edit(.:format) {:action=>"edit", :controller=>"blog/contexts"}
context GET /contexts/:id(.:format) {:action=>"show", :controller=>"blog/contexts"}
PUT /contexts/:id(.:format) {:action=>"update", :controller=>"blog/contexts"}
DELETE /contexts/:id(.:format) {:action=>"destroy", :controller=>"blog/contexts"}
Here is some good reading on the subject: http://edgeguides.rubyonrails.org/routing.html#controller-namespaces-and-routing
from the rails guide
"The namespace scope will automatically add :as as well as :module and :path prefixes."
so
namespace "admin" do
resources :contexts
end
is the same as
scope "/admin", as: "admin", module: "admin" do
resources :contexts
end
Both scope and namespace are scoping a set of routes to the given default options.
Except that there are no default options for scope, and for namespace
:path, :as, :module, :shallow_path and :shallow_prefix options all default to the name of the namespace.
Available options for both scope and namespace correspond to those of match.
scope is bit complex, but provides more options to fine-tune exactly what you want to do.
scope supports three options: module, path and as. If you see scope with all it options, it will be exactly same as namespace.
In other words, routes generated by
namespace :admin do
resources :posts
end
is same as
scope module: 'admin', path: 'admin', as: 'admin' do
resources :posts
end
In other words, we can say that there are no default options for scope as compared to namespace. namespace add all these options by default. Thus using scope, we can more fine tune the routes as required.
If you take a deep look into scope and namespace default behaviour, you will find that scope by default supports only :path option, where as namespace supports three options module, path and as by default.
For more info, please refer a doc namespace-and-routing.
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