Ruby on Rails中的动态路由设置,如Facebook

时间:2015-08-28 12:24:04

标签: ruby-on-rails ruby-on-rails-4 routes

当前路线已定义为:

Rails.application.routes.draw do
    namespace :users do
        resources :mapps
        resources :listings
        resources :likes
        get 'followers'  => 'connections#followers'
        get 'following' => 'connections#following'
        post 'unfollow' => 'connections#unfollow'
    end
    get ':username' => 'users#public_profile'
end

我想制作像facebook这样的路线:

   :username/:controller/:action => users/:controller/:action

例如,如果用户将URL命名为/ myusername / posts / 12,则请求必须转到用户文件夹内的控制器文件&用户:帖子

我见过许多相关问题,但没有使用Rails 4.2.3

1 个答案:

答案 0 :(得分:1)

来自http://guides.rubyonrails.org/routing.html#prefixing-the-named-route-helpers

的示例
scope ':username' do
        resources :mapps
        resources :listings
        resources :likes
        get 'followers'  => 'connections#followers'
        get 'following' => 'connections#following'
        post 'unfollow' => 'connections#unfollow' 
end