2017-04-13 82 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

相關問題