我正在使用acts_as_taggable_on 类固醇,我对生成标签链接的这段代码有问题:
<%= link_to tag, tag_path(:id => tag.name) %>
当我访问 URL 时:
http://localhost:3000/tags/rails
我得到错误:
No action responded to rails. Actions: show
但是,此 URL 有效:
http://localhost:3000/tags/show/rails
我在我的 tags_controller.rb 中定义了 show 动作
class TagsController < ApplicationController
def show
@stories = Story.find_tagged_with(params[:id])
end
end
我有以下由 rake:routes 生成的路线:
tags GET /tags(.:format) {:controller=>"tags", :action=>"index"}
POST /tags(.:format) {: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"}
所以我知道 URL tags/rails 指向路由 tags/:id,并且我为 link_to 提供了一个附加参数,以将标签名称分配为 :id 参数,但正如您所见,它不起作用。一个论坛建议我使用 to_param 但我没有 Tag 模型,这本书建议反对它。我错过了什么吗?
我正在关注 Sitepoint 的书《Simply Rails 2》
编辑:添加工作 URL,见顶部