如何为 Rails 应用程序创建应用程序范围的 slug 路由? [英] How to create app-wide slug routing for Rails app?

查看:35
本文介绍了如何为 Rails 应用程序创建应用程序范围的 slug 路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发的 Rails 应用中有许多不同的模型.我已经看到许多站点使用应用程序范围的 slug 路由方法.这是什么意思?

I have a number of different models in the Rails app I'm working on. I've seen a number of sites use an app-wide slug routing approach. What do I mean by this?

http://example.com/nick-oneill <-- This points to a User object
http://example.com/facebook    <-- This points to a Company object
http://example.com/developers  <-- This points to the users#index page

我知道 to_param 并在应用程序中创建对读者友好的 slug,但是我不知道有一种方法可以为各种对象设置根级 slug.您可以认为这与 Facebook 的 Graph API 类似:有不同的对象类型,但它们都存在于 https://graph.facebook.com/object-id

I'm aware of to_param and creating reader-friendly slugs within apps, however I'm not aware of an approach to have root-level slugs for a variety of objects. You can think of this being similar to Facebook's Graph API: there are different object types, but all of them exist at https://graph.facebook.com/object-id

任何见解将不胜感激!

推荐答案

使用 freindly_id 可能有办法做到这一点,但我认为友好 id 的问题是事物的范围受模型的限制.

There may be a way to do this with freindly_id but i think the problem with friendly id is things are scoped by model.

如果我想要真正的站点范围的 slugging,我会创建一个与我的所有模型都具有多态关系的 slugs 表.

If I wanted truely sitewide slugging I would create a slugs table with a polymorphic relationship to all my models.

Sluggable_type 和 sluggable_id,然后是带有完整永久链接/slug 的 slug 字段.

Sluggable_type and sluggable_id and then a slug field with the complete permalink/slug.

+---------------------------------------------+
| sluggable_type | sluggable_id |     slug    |
|      user      |       13     |  users/john |
+---------------------------------------------+

现在我可以做一个通配符捕获所有路由或在运行时为我的所有 slug 创建路由,并在更新受此 slugable 控制的模型时强制刷新路由.

Now i could do do a wildcard catch all route or create the routes for all my slugs at runtime and force a route refresh when a model is updated that was under this sluggable control.

routes.rb

  get "/*segments",
               :controller => 'slugs',
               :action => 'dynamicroute'

现在在你的 SlugsController 中实现一个类似的方法

Now in your SlugsController implement a method like

def dynamicroute
  segments = params[:segments]
  slugs.find_by_slug(segments)
  slug.sluggable_type.constantize.find(slug.sluggable_id) #retrive real record
  #some magic to handle the slugged item maybe redirect to the appropriate
  #controller or somehow call the show view for that controller
end

routes.rb

begin  
  Slug.all.each do |s|
    begin
      get "#{s.slug}" => "#{s.sluggable_type.demodulize.pluralize.camelize}#show"
    rescue
    end
  end
rescue
end

如果您使用第二种方法进行路由,请确保您调用

If you use the 2nd approach to routing make sure you call

YOUR_APP_NAME::Application.reload_routes!

编辑任何 slugged 记录后刷新路由表.

After editing any slugged record to refresh the routing table.

我们遇到了类似的问题,我们可能会尝试完善这种方法.

We've had similar issues and we may try our hand at gemifying this approach.

这篇关于如何为 Rails 应用程序创建应用程序范围的 slug 路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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