I came accross something strange when trying to create friendly URLs.
在尝试创建友好的URL时,我遇到了一些奇怪的事情。
I have an item model which has:
我有一个项目模型,其中包括:
def to_param
"#{id}-#{name}".parameterize
end
This makes my item URLs contain the "ID" and "name" just fine:
这使我的商品网址包含“ID”和“名称”就好了:
www.domain.com/items/ID-name
I also have a category model (item belongs to category and user models) where I have the same def to_param as above but the category URLs stay "unfriendly" no "name" included:
我也有一个类别模型(项目属于类别和用户模型),我有与上面相同的def to_param,但类别URL保持“不友好”,不包括“名称”:
domain.com/categories/ID
I have name column in category table and it has values. I also use ancestry for the category model. Maybe has_ancestry is causing the issue?
我在类别表中有名称列,它有值。我还使用祖先作为类别模型。也许has_ancestry造成了这个问题?
I tried below but no luck:
我试过下面但没有运气:
def to_param
[id, name.parameterize].join("-")
end
Thanks for any advice!
谢谢你的建议!
1 个解决方案
#1
Papirtiger's comment lead me to the solution. The problem was I had links like:
Papirtiger的评论引导我找到解决方案。问题是我有这样的链接:
<% @category.children.in_groups_of(4, false) do |childs| %>
<tr>
<% for categories in childs %>
<td>
<%= link_to "../categories/#{categories.id}" do %><%= categories.name %><% end %></td>
<% end %>
</tr>
Beginners solution. :)
初学者解决方案。 :)
Removed the ugly part and now it works:
删除丑陋的部分,现在它的工作原理:
<% @category.children.in_groups_of(4, false) do |childs| %>
<tr>
<% for categories in childs %>
<td><%= link_to categories do %><%= categories.name %><% end %> (<%= Item.where(:category_id => categories.id).count %>)</td>
<% end %>
</tr>
#1
Papirtiger's comment lead me to the solution. The problem was I had links like:
Papirtiger的评论引导我找到解决方案。问题是我有这样的链接:
<% @category.children.in_groups_of(4, false) do |childs| %>
<tr>
<% for categories in childs %>
<td>
<%= link_to "../categories/#{categories.id}" do %><%= categories.name %><% end %></td>
<% end %>
</tr>
Beginners solution. :)
初学者解决方案。 :)
Removed the ugly part and now it works:
删除丑陋的部分,现在它的工作原理:
<% @category.children.in_groups_of(4, false) do |childs| %>
<tr>
<% for categories in childs %>
<td><%= link_to categories do %><%= categories.name %><% end %> (<%= Item.where(:category_id => categories.id).count %>)</td>
<% end %>
</tr>