如何在 Rails 3 中使用用户定义的友好 URL?
现在我有这样的东西
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
FriendlyID 是一个很棒的宝石。
实现用户定义的页面 URL 应该不难。
使用
user_id
和link
创建表pages
在
page#new
上为link 添加输入
属性。或者,您可以使用 :use_slug => 在标题或其他内容上设置Friendly_id真正的选择。这样,FriendlyID 将获取标题并对其进行修改,使其不包含受限制的字符。它将使用自己的表来存储 slugs。使用
cached_slug
来提高性能。已更新
要让用户选择是否要设置自定义链接,您可以执行以下操作:
link
字段上设置Friendly_id(不带 slugs)。permalink
这样您就可以在表单中显示它。permalink
。link
字段。link
字段。FriendlyID 使用
babosa
gem 生成 slugs。如果您也决定使用它,您的过滤器将如下所示:FriendlyID is a great gem.
It shouldn't be hard to implement user defined page URL.
Create table
pages
withuser_id
andlink
On the
page#new
you add an input for thelink
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:
link
field without slugs..permalink
so you could show it in your forms.permalink
is set.link
field.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:将 to_param 方法添加到模型之一应该会有所帮助:
希望这就是您正在寻找的:)
Adding to_param method to one of the models should help:
Hope this is what you are looking for :)
我没有使用Friendly_url gem,并且不确定我的方法是否有效。但它对我来说效果很好。
我有一个名为
Node
的模型,其中包含id
和名为url_title
的友好 url 字段。我的
routes.rb
文件:nodes_controller.rb
并使用
@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
withid
and friendly url field calledurl_title
.My
routes.rb
file:nodes_controller.rb
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 forfriendly_url
.Feel free to correct me if I am wrong in these assumptions.
有多种方法可以实现这一目标
- 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!