在 ruby on Rails 中将参数作为路径变量传递
我对 ROR 还是个新手,所以请原谅这个问题的简单性...
所以 http:// www.example.com/controller/:id 在我的表中显示一条记录,其中 :id 是数字(1、2、3 等)。
有没有办法让 URL 中的 :id 成为显示记录中字段的值? 这样我就可以拥有http://www.example.com/controller/record_field? 我希望对表中的特定记录有一个人性化的参考。 我确信这一定是可能的。 我需要更改routes.rb 中的某些内容吗?
谢谢您的帮助!
I'm still new to ROR, so pardon the simplicity of the question...
So http://www.example.com/controller/:id displays a record in my table, with :id being a number (1,2,3 etc.).
Is there a way I can have :id in the URL be the value of a field in the displayed record? Such that I can have http://www.example.com/controller/record_field? I want to have a human-friendly reference to specific records in my table. I'm sure this must be possible. Do I change something in routes.rb?
Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最干净的方法是在模型中添加一个新的 find 方法(或者简单地使用 Rails 为您提供的 find_by_fieldname 控制)。 然后,您将让控制器使用该方法而不是常规 find(params[:id]) 来提取模型记录。
请在此处查看 Ryan B 的截屏视频。 这很容易,而且他是一位好老师,所以你不应该有任何问题。
The cleanest way is to add a new find method in your model (or simply use the find_by_fieldname Rails gives you in your control). Then you'll have your controller use that method instead of the regular find(params[:id]) to pull your model record.
Check out Ryan B's screencast on this here. It's pretty easy, and he's a good teacher, so you shouldn't have any problems.
我使用名为Friendly_id 的优秀rails 插件。
http://github.com/norman/Friendly_id/tree/master
这应该排序你出去得很好。 它也有详细记录。
请注意可能具有现代希腊字符的字段 - 可能需要为这些字符找到解决方案。
I use the excellent rails plugin named friendly_id.
http://github.com/norman/friendly_id/tree/master
That should sort you out nicely. It is well documented too.
Take care around fields that might have modern Greek characters—might need to figure a work around for those.
乔恩·斯莫克(Jon Smock)的解决方案也将发挥作用。 我倾向于更喜欢以下内容。
宽泛地说,这是一种 SEO 技术(漂亮的 URL 也是用户友好的,我向所有人推荐它们,即使您不关心 SEO,例如在登录后的页面上)。 我对 Rails SEO 有更深入的讨论,其中包括类似的其他技巧,此处。
重要提示:您应该在设计时考虑如果
param
发生更改您将要做什么。 例如,在我的汉堡场景中,我完全有可能将“Sinously Delicious Cheeseburger”重命名为“Triple Bypass”。 如果 URL 发生变化,可能会产生一些影响,例如指向我网站的客户链接被破坏。 因此,对于生产用途,我通常为这些模型提供一个不可变的永久链接属性,我将其初始化为人类有意义的一次。 如果对象稍后发生变化,那么 URL 保持不变。 (还有其他解决方案——这只是最简单的一个。)Jon Smock's solution will work, too. I tend to prefer the following.
This is, loosely speaking, an SEO technique (beautiful URLs are also user-friendly and I suggest them to absolutely everyone even if you don't care about SEO, for example on pages behind a login). I have a more extended discussion of Rails SEO, which includes other tips like this, here.
Important tip: You should consider, at design-time, what you are going to do if the
param
should change. For example, in my hamburger scenario, it is entirely possible that I might rename "Sinfully Delicious Cheeseburger" to "Triple Bypass". If that changes URLs, there are some possible implications, such as breakage of customer links to my website. Accordingly, for production use I usually give these models an immutablepermalink
attribute which I initialize to be human-meaningful exactly once. If the object later changes, oh well, the URL stays the same. (There are other solutions -- that is just the easiest one.)