在 ruby​​ on Rails 中将参数作为路径变量传递

发布于 2024-07-08 15:00:18 字数 465 浏览 4 评论 0原文

我对 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 技术交流群。

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

发布评论

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

评论(3

大姐,你呐 2024-07-15 15:00:18

最干净的方法是在模型中添加一个新的 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.

爱给你人给你 2024-07-15 15:00:18

我使用名为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.

疯了 2024-07-15 15:00:18

乔恩·斯莫克(Jon Smock)的解决方案也将发挥作用。 我倾向于更喜欢以下内容。

class Hamburger << ActiveRecord::Base

  #this normally defaults to id
  def to_param 
     name
  end

end

class SomeModelController << ApplicationController

  def show 
    @hamburger = Hamburger.find(params[:id])  #still default code
  end
end

#goes in some view
This is the <%= link_to "tastiest hamburger ever", url_for(@hamburger) %>.

宽泛地说,这是一种 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.

class Hamburger << ActiveRecord::Base

  #this normally defaults to id
  def to_param 
     name
  end

end

class SomeModelController << ApplicationController

  def show 
    @hamburger = Hamburger.find(params[:id])  #still default code
  end
end

#goes in some view
This is the <%= link_to "tastiest hamburger ever", url_for(@hamburger) %>.

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 immutable permalink 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.)

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