我正在使用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中定义了表演动作
class TagsController < ApplicationController
def show
@stories = Story.find_tagged_with(params[:id])
end
end我有以下由rake生成的路径:路由:
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标记/rails指向路由标记/ :id,我还为link_to提供了一个额外的param来将标记名指定为:id,但正如您所看到的,它不起作用。一个论坛建议我使用to_param,但我没有标签模型和书建议反对它。我有遗漏什么吗?
我遵循的是Sitepoint的书--简单地说是Rails 2
编辑:添加了工作URL,请参见顶部
发布于 2011-02-17 21:47:11
尝试将其添加到路由资源中:
:requirements => { :id => /.*/ }发布于 2011-02-20 06:03:14
在黑暗中射击,但应该
<%= link_to tag, tag_path(:id => tag.name) %>
be
<%= link_to tag, tag_path(:id => tag.id) %>
或
<%= link_to tag, tag_path(tag) %>
发布于 2011-02-20 06:20:52
试试这个链接:
link_to tag.name, { :action => :tag, :id => tag.name }我不知道你使用的是什么版本的rails,我假设是3。
基本上,您使用的是离开id的tag_path。如果您没有更改任何内容,这意味着类似于tag/43,标记为id 43。建议您重写to_param的原因是,如果您希望它离开标记的名称,那么类似于tag/rails。为此,你要做这样的事情:
class Tag
def to_param
name
end
end最后,您必须更改show操作以使用名称,而不是id。所以@stories = Story.find_tagged_with(params[:name])。然后,我相信您会想要创建一个路径来弥补这一点,所以在您的resources :tags之上添加match "/tags/:name" => "tags#show"。
https://stackoverflow.com/questions/5035011
复制相似问题