如何实现“短"在rails中嵌套虚荣网址? [英] How to implement "short" nested vanity urls in rails?

查看:10
本文介绍了如何实现“短"在rails中嵌套虚荣网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解如何在 Rails 中创建虚 URL 以进行翻译http://mysite.com/forum/1 变成 http://mysite.com/some-forum-name

I understand how to create a vanity URL in Rails in order to translate http://mysite.com/forum/1 into http://mysite.com/some-forum-name

但我想更进一步,让以下工作(如果可能的话):

But I'd like to take it a step further and get the following working (if it is possible at all):

而不是:http://mysite.com/forum/1/board/99/thread/321

我想在第一步得到这样的东西:http://mysite.com/1/99/321

I'd like in the first step to get to something like this: http://mysite.com/1/99/321

并最终像 http://mysite.com/some-forum-name/some-board-name/this-is-the-thread-subject 一样拥有它.

and ultimately have it like http://mysite.com/some-forum-name/some-board-name/this-is-the-thread-subject.

这可能吗?

推荐答案

要使用 Rails URL 帮助程序很好地"完成这项工作,您必须在模型中覆盖 to_param:

To have this work "nicely" with the Rails URL helpers you have to override to_param in your model:

def to_param
  permalink
end

permalink 可能由 before_save

before_save :set_permalink

def set_permalink
  self.permalink = title.parameterize
end

您创建永久链接的原因是,最终,也许,可能,您将拥有一个不适合 URL 的标题.这就是 parameterize 的用武之地.

The reason you create a permalink is because, eventually, maybe, potentially, you'll have a title that is not URL friendly. That is where parameterize comes in.

现在,至于根据 permalink 的内容查找这些帖子,您可以走简单的路线,也可以走困难的路线.

Now, as for finding those posts based on what permalink is you can either go the easy route or the hard route.

轻松路线

定义 to_param 略有不同:

def to_param
  id.to_s + permalink
end

继续使用 Forum.find(params[:id]) 其中 params[:id] 将是诸如 1-my-awesome-forum 之类的东西.为什么这仍然有效?好吧,Rails 将在传递给 find 的参数上调用 to_i,并且在该字符串上调用 to_i 将简单地返回 1.

Continue using Forum.find(params[:id]) where params[:id] would be something such as 1-my-awesome-forum. Why does this still work? Well, Rails will call to_i on the argument passed to find, and calling to_i on that string will return simply 1.

艰难路线

to_param 保持不变.在你的控制器中使用 find_by_permalink,使用 params[:id],它是通过路由传递的:

Leave to_param the same. Resort to using find_by_permalink in your controllers, using params[:id] which is passed in form the routes:

Model.find_by_permalink(params[:id])

现在是有趣的部分

现在您想从 URL 中取出资源.嗯,这是一种西西弗斯式的方法.当然,您可以停止使用 Ruby on Rails 提供的路由帮助器,例如 map.resources 并使用 map.connect 定义它们,但这真的值得吗这么大的收获?它赋予你什么特殊的超能力"?恐怕没有.

Now you want to take the resource out of the URL. Well, it's a Sisyphean approach. Sure you could stop using the routing helpers Ruby on Rails provides such as map.resources and define them using map.connect but is it really worth that much gain? What "special super powers" does it grant you? None, I'm afraid.

但如果你想这样做,这里是一个很好的起点:

But still if you wanted to do that, here's a great place to start from:

get ':forum_id/:board_id/:topic_id', :to => "topics#show", :as => "forum_board_topic"

这篇关于如何实现“短"在rails中嵌套虚荣网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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