loxosceles
loxosceles

Reputation: 356

Route devise to different controller/view set

I'm trying to achieve the following:

I have a simple page where a visitor can view a list of albums (index action) and by clicking on any of the albums, view the photos of every album (show action). No other actions (edit, new, update, destroy) should be reachable.

When the user logs in she can see the index and show pages, plus all the other actions should be available, but now index and show will lock differently. The URLs shouldn't show up any different from before, though.

I created these routes:

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

I also created user directories under the app/controllers and app/views directories where I placed the namespaced (logged in) user controllers and views.

So now for the visitor (no login) the default controller/views should be used but once the user signs in (using devise) the controller/views under the user directory should take over.

How can I put this redirection in place?

This is my routes.rb so far:

  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"

Upvotes: 0

Views: 581

Answers (1)

idej
idej

Reputation: 5674

As I understand you need to redirect users only after login if different views/controllers are used. You can use devise after_sign_in_path_for method. Add it to your application controller:

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

But for allow/deny actions or show/hide links better approach use something like gem pundit and avoid DRY.

Upvotes: 1

Related Questions