首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails link_to与acts_as_taggable_on_steroids

Rails link_to与acts_as_taggable_on_steroids
EN

Stack Overflow用户
提问于 2011-02-17 21:40:43
回答 4查看 557关注 0票数 2

我正在使用acts_as_taggable_on类固醇,在生成标记链接的这段代码中,我遇到了问题:

代码语言:javascript
复制
<%= link_to tag, tag_path(:id => tag.name) %>

当我访问URL时:

代码语言:javascript
复制
http://localhost:3000/tags/rails

我知道错误:

代码语言:javascript
复制
No action responded to rails. Actions: show

但是,此URL工作如下:

代码语言:javascript
复制
http://localhost:3000/tags/show/rails

我已经在我的tags_controller.rb中定义了表演动作

代码语言:javascript
复制
class TagsController < ApplicationController
  def show
    @stories = Story.find_tagged_with(params[:id])
  end
end

我有以下由rake生成的路径:路由:

代码语言:javascript
复制
           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,请参见顶部

EN

回答 4

Stack Overflow用户

发布于 2011-02-17 21:47:11

尝试将其添加到路由资源中:

代码语言:javascript
复制
:requirements => { :id => /.*/ }
票数 0
EN

Stack Overflow用户

发布于 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) %>

票数 0
EN

Stack Overflow用户

发布于 2011-02-20 06:20:52

试试这个链接:

代码语言:javascript
复制
link_to tag.name, { :action => :tag, :id => tag.name }

我不知道你使用的是什么版本的rails,我假设是3。

基本上,您使用的是离开id的tag_path。如果您没有更改任何内容,这意味着类似于tag/43,标记为id 43。建议您重写to_param的原因是,如果您希望它离开标记的名称,那么类似于tag/rails。为此,你要做这样的事情:

代码语言:javascript
复制
class Tag
  def to_param
    name
  end
end

最后,您必须更改show操作以使用名称,而不是id。所以@stories = Story.find_tagged_with(params[:name])。然后,我相信您会想要创建一个路径来弥补这一点,所以在您的resources :tags之上添加match "/tags/:name" => "tags#show"

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5035011

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档