2017-04-13 87 views
0

我试图实现以下目标:路线设计不同的控制器/视图设置

我有一个简单的页面,访问者可以浏览的相册(index动作)的列表,并通过点击任何专辑,查看每张专辑的照片(显示操作)。没有其他操作(编辑,新建,更新,销毁)应该可以访问。

当用户登录时,她可以看到索引和显示页面,再加上所有其他操作应该可用,但现在索引和显示将锁定不同。不过,这些网址不应该显示与以前不同的地方。

我创建这些路线:

users_albums GET /users/albums(.:format)   users/albums#index 
users_album  GET /users/albums/:id(.:format)  users/albums#show 
new_users_album GET /users/albums/new(.:format)  users/albums#new 
.... and so on ... 
albums   GET /albums(.:format)    albums#index 
album   GET /albums/:id(.:format)   albums#show 

我还创建下app/controllersapp/views目录user目录哪里放置命名空间(登录)的用户的控制器和视图。

所以现在访问者(无需登录)应该使用默认的控制器/视图,但是一旦用户登录(使用设计)user目录下的控制器/视图应该接管。

我该如何把这个重定向到位?

这是我的routes.rb至今:

devise_for :users, skip: [:registrations] 
    as :user do 
    get "/sign_in" => "devise/sessions#new" # custom path to login/sign_in 
    get "/sign_up" => "devise/registrations#new", as: "new_user_registration" # custom path to sign_up/registration 
    get 'users/edit' => 'devise/registrations#edit', :as => 'edit_user_registration' 
    put 'users' => 'devise/registrations#update', :as => 'user_registration' 
    end 

    namespace :users do 
    resources :albums do 
     resources :photos 
    end 
    end 

    resources :albums, only: [:index, :show] do 
    resources :photos, only: [:index, :show] 
    end 

    root to: "albums#index" 

回答

1

据我了解,你需要如有不同意见/使用控制器仅在登录之后将用户重定向。你可以使用设计after_sign_in_path_for的方法。将它添加到您的应用程序控制器:

def after_sign_in_path_for(resource) 
    users_albums_path #or any route that you want use 
end 

但允许/拒绝动作或显示/隐藏链接更好的方法使用的东西,如宝石般pundit,避免干燥。

+0

我之前做过相当多的研究,但没有遇到过这个。使它变得非常简单,只需要改变一些链接,我就全部设置好了。 – loxosceles

相关问题