0

根据this guide,我正在使用to_param为我的对象创建友好的URL。我从它使用to_param创建具有标签的名称,而不是它的ID网址acts_as_taggable_on创业板Tag对象:现在to_param仅在某些控制器中正常运行

class Tag < ActiveRecord::Base 

    def to_param 
    name 
    end 

    def self.find_by_param(input) 
    find_by_name(input) 
    end 
end 

,在我的应用程序有几个地方我我所链接的页面此标记其中显示所有关联的对象。在我的主页,下Static控制器(和其他控制器),链接到一个Tag对象的行为正确:

_tag_item.html.erb

<span class="tag"><%= link_to "##{tag_item}", tag_path(tag_item) %></span> 

正常生产:

<span class="tag"><a href="/tags/gandalf">#gandalf</a></span> 

注意如何href指向友好的URL,而不是默认使用ID。

但是,在Tag控制器本身的标签'index页面中列出了应用程序中的所有标签,但此行为的行为并不正确。

views/tags/index.html.erb

<% @tags.each do |tag| %> 
    <div class="tag-box"> 
     <%= link_to tag, tag_path(tag) %> 
    </div> 
<% end %> 

错误产生:

<a href="/tags/2">gandalf</a> 

,我不知道为什么。

回答

-1

controllers/tags/index.html.erb变化

<%= link_to tag, tag_path(tag) %> 

<%= link_to "##{tag}", tag_path(tag) %> 
+0

我并不想以一个主题标签添加到链接的文本。我试图让'link_to'正确使用我在别处指定的友好URL。关键是'link_to'在某些地方输出'/ tags/gandalf',在其他地方输出'/ tags/1'。 – Polyov

+0

好吧,我的坏。你可以调试循环中的'tag'对象吗?它是否是'Tag'的一个实例。 – Sunny

+0

它是'ActsAsTaggableOn:Tag'的一个实例,这是我用于标记的gem。 – Polyov

相关问题