You have two routes which point to posts#show
(you should be able to confirm this by running rake routes
), and your link is using the wrong one.
When you call link_to('show', post)
the URL of the link is generated by calling url_for(post)
which (eventually, after passing through several other methods on the way) calls post_path(post)
. Since the route to posts#show
that was created by your call to resources(:posts)
is named post
, that is the route that post_path
generates.
You also currently have inconsistent routes for the show, update and destroy actions which will probably cause you problems later on.
You can fix this by changing your routes to the following:
resources :posts, :except => ['show', 'update', 'destroy']
get 'posts/:id/:slug' => 'posts#show', :as => 'post'
put 'posts/:id/:slug' => 'posts#update'
delete 'posts/:id/:slug' => 'posts#destroy'
Unfortunately you still can't use link_to('show', post)
just yet, because it relies on being able to use post.to_param
as the single argument needed to build a path to a post. Your custom route requires two arguments, an id
and a slug
. So now your link code will need to look like this:
link_to 'show', post_path(post.id, post.slug)
You can get around that problem by defining your own post_path
and post_url
helpers in app/helpers/posts_helper.rb
:
module PostsHelper
def post_path(post, options={})
post_url(post, options.merge(:only_path => true))
end
def post_url(post, options={})
url_for(options.merge(:controller => 'posts', :action => 'show',
:id => post.id, :slug => post.slug))
end
end
Which means we're finally able to use:
link_to 'show', post
If that all seems like too much work, a common alternative is to use URLs that look more like posts/:id-:slug
, in which case you can stick with the standard RESTful routes and just override the to_param
method in your Post
class:
def to_param
"#{id}-#{slug}"
end
You'll also need to do a little bit of work splitting up params[:id]
into an ID and a slug before you can look up the relevant instance in your show, edit, update and destroy controller actions.