如何在 Rails 3 中使用用户定义的友好 URL?

发布于 2024-11-24 01:54:34 字数 410 浏览 6 评论 0原文

现在我有这样的东西

http://myapp.com/pages/1
http://myapp.com/pages/2
http://myapp.com/pages/3
http://myapp.com/pages/4

,每个页面都属于一个用户

我需要的是每个用户为页面设置自己的自定义名称。

我正在考虑使用 Friendly_id gem http://norman.github.com/Friendly_id / 但我没有找到任何方法来直接编辑 slug 以设置自定义友好 url

我应该如何继续?

Now i have something like this

http://myapp.com/pages/1
http://myapp.com/pages/2
http://myapp.com/pages/3
http://myapp.com/pages/4

And each page belong to one user

What i need is to each user to set it's own custom name for the page.

I was thinking of using the friendly_id gem http://norman.github.com/friendly_id/
but I don't find any method to directly edit the slug to set a custom friendly url

how should i proceed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烟─花易冷 2024-12-01 01:54:34

FriendlyID 是一个很棒的宝石。

实现用户定义的页面 URL 应该不难。
使用 user_idlink 创建表 pages

class User < ActiveRecord::Base
  has_many :pages

class Page < ActiveRecord::Base
  belongs_to :user

  has_friendly_id :link # link is name of the column whose value will be replaced by slugged value

page#new 上为 link 添加输入 属性。

或者,您可以使用 :use_slug => 在标题或其他内容上设置Friendly_id真正的选择。这样,FriendlyID 将获取标题并对其进行修改,使其不包含受限制的字符。它将使用自己的表来存储 slugs。使用cached_slug来提高性能。

已更新

要让用户选择是否要设置自定义链接,您可以执行以下操作:

  1. link 字段上设置Friendly_id(不带 slugs)。
  2. 创建虚拟属性 permalink 这样您就可以在表单中显示它。
  3. 在before_filter中,检查是否设置了permalink
  4. 如果是,请将其写入 link 字段。
  5. 如果不是,请将标题写入 link 字段。

FriendlyID 使用 babosa gem 生成 slugs。如果您也决定使用它,您的过滤器将如下所示:

protected
  def generate_link
                #you might need to use .nil? instead
    self.link = self.permalink.empty? ? make_slug(self.title) : make_slug(self.permalink)
  end

  def make_slug(value)
    value.to_slug.normalize.to_s #you could as well use ph6py's way
  end

FriendlyID is a great gem.

It shouldn't be hard to implement user defined page URL.
Create table pages with user_id and link

class User < ActiveRecord::Base
  has_many :pages

class Page < ActiveRecord::Base
  belongs_to :user

  has_friendly_id :link # link is name of the column whose value will be replaced by slugged value

On the page#new you add an input for the link attribute.

Alternatively, you could set friendly_id on title or something else with :use_slug => true option. This way FriendlyID will take the title and modify it so it doesn't have and restricted characters. It will use it's own table to store slugs. Use cached_slug to increase performanse.

Updated

To give users a choice whether they wan't to set a custom link, you could do this:

  1. Set friendly_id on the link field without slugs..
  2. Make a virtual attribute permalink so you could show it in your forms.
  3. In the before_filter, check whether the permalink is set.
  4. If it is, write it to the link field.
  5. If it's not, write title to the link field.

FriendlyID uses babosa gem to generate slugs. If you decide to use it as well, this is how your filter could look like:

protected
  def generate_link
                #you might need to use .nil? instead
    self.link = self.permalink.empty? ? make_slug(self.title) : make_slug(self.permalink)
  end

  def make_slug(value)
    value.to_slug.normalize.to_s #you could as well use ph6py's way
  end
只想待在家 2024-12-01 01:54:34

将 to_param 方法添加到模型之一应该会有所帮助:

def to_param
  "#{id}-#{call_to_method_that_returns_custom_name.parameterize}"
end

希望这就是您正在寻找的:)

Adding to_param method to one of the models should help:

def to_param
  "#{id}-#{call_to_method_that_returns_custom_name.parameterize}"
end

Hope this is what you are looking for :)

淡淡離愁欲言轉身 2024-12-01 01:54:34

我没有使用Friendly_url gem,并且不确定我的方法是否有效。但它对我来说效果很好。

我有一个名为 Node 的模型,其中包含 id 和名为 url_title 的友好 url 字段。

我的routes.rb 文件:

resource 'nodes/:url_title', :to => 'Nodes#view'

nodes_controller.rb

class NodesController <ActiveController
 def view
  @node = Node.find_by_url_title(:params(url_title))
 end
end

并使用@node 变量来填充您的视图。

现在,每当我输入 www.example.com/nodes/awesome-title 时,它​​都会带我到正确的页面。反对这一点的一个论点是需要在非主字段上创建索引。但我认为,即使在Friendly_url gem 中,这也可能需要更好的性能。此外,非主字段 url_title 需要是唯一的。同样,即使要正确工作 friend_url 也可能需要这样做。
如果我的这些假设有误,请随时纠正我。

I am not using the friendly_url gem and am not sure whether my way is efficient. But it works fine for me.

I have a model called Node with id and friendly url field called url_title.

My routes.rb file:

resource 'nodes/:url_title', :to => 'Nodes#view'

nodes_controller.rb

class NodesController <ActiveController
 def view
  @node = Node.find_by_url_title(:params(url_title))
 end
end

And use the @node variable to populate your view.

Now, whenever I type www.example.com/nodes/awesome-title , it takes me to the proper page. One argument against this can be need to create an index on a non-primary field. But I think that might be required for better performance even in the friendly_url gem. Also, the non-primary field url_title needs to be unique. Again, this might be required even for correct working for friendly_url .
Feel free to correct me if I am wrong in these assumptions.

只涨不跌 2024-12-01 01:54:34

有多种方法可以实现这一目标

- 1)使用 Stringex

2)sluggable-finder

3)Friendly_id

可以找到完整的分步方法以及每种方法的使用原因 此处。阅读愉快!

There are a variety of ways, you can achieve this-

1) using Stringex

2) sluggable-finder

3) friendly_id

A complete step by step methodology with reasons for each to be used can be found out here. Happy reading!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文