Rails link_to with acts_as_taggable_on_steroids
I am using acts_as_taggable_on steroids and I am having problem with this piece of code that generates a link to a tag:
<%= link_to tag, tag_path(:id => tag.name) %>
when I access the URL:
http://localhost:3000/tags/rails
I get the error:
No action responded to rails. Actions: show
However, this URL works:
http://localhost:3000/tags/show/rails
I have defined the show action in my tags_controller.rb
class TagsController < ApplicationController
def show
@stories = Story.find_tagged_with(params[:id])
end
end
I have the following routes generated by rake:routes :
tags GET /tags(.:format) {:controller=>"tags", :action=>"index"}
POST /tags(.:fo开发者_运维知识库rmat) {:controller=>"tags", :action=>"create"}
new_tag GET /tags/new(.:format) {:controller=>"tags", :action=>"new"}
edit_tag GET /tags/:id/edit(.:format) {:controller=>"tags", :action=>"edit"}
tag GET /tags/:id(.:format) {:controller=>"tags", :action=>"show"}
PUT /tags/:id(.:format) {:controller=>"tags", :action=>"update"}
DELETE /tags/:id(.:format) {:controller=>"tags", :action=>"destroy"}
so I know that URL tags/rails points to the route tags/:id, and I've provided an additional param to link_to to assign the tag name as the :id param, but as you can see, it's not working. A forum suggested I use the to_param but I have not Tag model and the book suggested against it. Am I missing anything?
I am following the Sitepoint book Simply Rails 2
EDIT: added working URL, see top
Try adding this to your route resource:
:requirements => { :id => /.*/ }
Shooting in the dark here but should
<%= link_to tag, tag_path(:id => tag.name) %>
be
<%= link_to tag, tag_path(:id => tag.id) %>
or
<%= link_to tag, tag_path(tag) %>
Try this for your link:
link_to tag.name, { :action => :tag, :id => tag.name }
I don't know what version of rails you're using, I'm assuming 3.
Basically, you're using the tag_path which goes off of the id. If you haven't changed anything, that means something like tag/43
, tag with id 43. The reason you were suggested to override to_param
is in case you want it to go off the name of the tag instead, so something like tag/rails
. For that, you do something like this:
class Tag
def to_param
name
end
end
Finally, you will have to change the show action to use the name, not the id. So @stories = Story.find_tagged_with(params[:name])
. Then I believe that you will want to create a route to compensate for this, so above your resources :tags
, add match "/tags/:name" => "tags#show"
.
For me it looks like the difference in routes.rb between
resources :tags
and
resource :tags
The first will have as its default index action, the second will not have :index, but it will respond with show on default route.
精彩评论