使用 act_as_taggable_on 为标签创建 url slug [英] Creating url slugs for tags with acts_as_taggable_on

查看:39
本文介绍了使用 act_as_taggable_on 为标签创建 url slug的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为由acts_as_taggable_on gem 管理的标签创建url slug.例如,而不是像 http://myapp.com/tags/5 这样的网址,我想要http://myapp.com/tags/my-tag(其中我的标签"是标签的唯一名称).

I would like to create url slugs for tags managed by the acts_as_taggable_on gem. For instance instead of urls like http://myapp.com/tags/5, I would like to have http://myapp.com/tags/my-tag (where 'my tag' is the tag's unique name).

在我自己创建的模型中,我通常通过覆盖模型的 to_param 方法来做到这一点,并在模型中创建一个slug"字段来保存新的 to_param 方法的结果.我尝试使用 ActsAsTaggableOn 的标签模型来执行此操作,但它不起作用.

In models that I create myself I usually do this by overriding the model's to_param method, and creating a "slug" field in the model to save the result of the new to_param method. I tried doing this with the Tag model of ActsAsTaggableOn, but it is not working.

否则我可以按如下方式覆盖 ActsAsTaggableOn 的 tag.rb 类中的内容:

I can otherwise override things in the tag.rb class of ActsAsTaggableOn as follows:

# Overwrite tag class
ActsAsTaggableOn::Tag.class_eval do
  def name
    n = read_attribute(:name).split
    n.each {|word| word.capitalize!}.join(" ")
  end      
end

但是,如果我尝试使用以下方法定义覆盖同一块中的 to_param 方法:

However, if I try to override the to_param method in that same block with a method definition like:

def to_param
  name.parameterize
end

Rails 仍然使用整数 ID 而不是参数化名称生成和响应路由.实际上在控制台中,如果我尝试类似

Rails still generates and responds to routes with integer IDs rather than the parameterized name. In fact in the console if I try something like

ActsAsTaggableOn::Tag.find(1).to_param

返回的是整数 ID,而不是重写的 to_param 方法的结果.

The integer ID is returned, rather than the result of the overridden to_param method.

如果有任何方法可以使用我自己的应用程序代码来实现,我宁愿不分叉 gem 并对其进行自定义.谢谢.

I'd rather not fork the gem and customize it if there is any way I can do it with my own application code. Thanks.

推荐答案

我正在使用friendly_id ( https://github.com/norman/friendly_id ) gem 来管理 slug.我为标签创建 slug 的方法与您的类似,但更简单一些.

I'm using the friendly_id ( https://github.com/norman/friendly_id ) gem to manage slugs. My method to create slugs for my tags is similar to yours, but a lit bit simpler.

我刚刚使用以下代码创建了初始值设定项 act_as_taggable_on.rb:

I've just created the initializer act_as_taggable_on.rb with the following code:

# act_as_taggable_on.rb
ActsAsTaggableOn::Tag.class_eval do
  has_friendly_id :name,
                  :use_slug => true,
                  :approximate_ascii => true,
                  :reserved_words => ['show', 'edit', 'create', 'update', 'destroy']
end

然后:

@user = User.new :name => "Jamie Forrest"
@user.tag_list = "This is awesome!, I'm a ruby programmer"
@user.save

瞧:

ActsAsTaggableOn::Tag.find('this-is-awesome')    #=> #<Tag id: 1, name: "This is awesome!">
ActsAsTaggableOn::Tag.find('im-a-ruby-programmer')    #=> #<Tag id: 2, name: "I'm a ruby programmer">

希望这有助于...

这篇关于使用 act_as_taggable_on 为标签创建 url slug的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆