【问题标题】:Rails routing like github像 github 一样的 Rails 路由
【发布时间】:2012-07-17 17:40:32
【问题描述】:

我正在使用 Rails 3.2

我希望路由与 github 非常相似,所以:

root/(username)
root/(username)/(projectname)
root/(username)/(projectname)/issus

等等

我正在尝试这样的事情:

resources :publishers do
  resources :magazines do
    resources :photos
  end
end 

但这给出了这样的路线:

/publishers/1/magazines/2/photos/3

我正在查看的项目执行以下似乎可行但似乎不适合我的项目。

resources :projects, :constraints => { :id => /[^\/]+/ }, :except => [:new, :create, :index], :path => "/" do
member do
  get "team"
  get "wall"
  get "graph"
  get "files"
end

resources :wikis, :only => [:show, :edit, :destroy, :create] do
  member do
    get "history"        
  end
end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2 rails-routing


    【解决方案1】:

    如果您想摆脱 id 编号(这是 rails 默认值)并使用我建议使用 FriendlyId gem 的名称。

    观看此 railscast http://railscasts.com/episodes/314-pretty-urls-with-friendlyid

    这里是github页面https://github.com/norman/friendly_id

    编辑

    这是我要找的文章,我忘了几个月前我把它收藏了。 http://jasoncodes.com/posts/rails-3-nested-resource-slugs

    【讨论】:

    • 看起来很有趣。我得去看看那个railscast。我想知道它是如何在内部存储的,以及使用不同的数据库是否重要。
    • 这很酷。不完全是我想要的,但会有所帮助。我更想要的是 github 是如何工作的。这样您就不需要像 /users/Jonovono 这样的网址,而只需 /Jonovono 就可以了。
    • 漂亮!!完美运行。非常感谢!
    【解决方案2】:

    您必须使用friendly_id 和范围

    scope '/:username/:projectname', module: 'users/projects', as: 'users_project' do
        resources :issus
        resources :photos
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-25
      • 1970-01-01
      • 2014-09-25
      • 2011-02-01
      • 2015-11-23
      • 2011-04-10
      • 1970-01-01
      相关资源
      最近更新 更多