将设计路由到不同的控制器/视图集 [英] Route devise to different controller/view set

查看:75
本文介绍了将设计路由到不同的控制器/视图集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力实现以下目标:

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.

当用户登录时,她可以看到索引并显示页面,以及所有其他操作应该可用,但现在index和show的锁定将不同。但是,URL不应显示任何不同。

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.

我创建了以下路线:

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 / controllers app / views 下的 user 目录我放置命名空间(登录)的用户控制器和视图的目录。

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

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

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.

我该如何p

到目前为止,这是我的route.rb:

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"


推荐答案

据我了解,您需要如果使用了不同的视图/控制器,则仅在登录后才重定向用户。您可以使用devise after_sign_in_path_for 方法。将其添加到应用程序控制器中:

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

但是对于允许/拒绝操作或显示/隐藏链接,更好的方法是使用诸如gem 专家,并避免DRY。

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

这篇关于将设计路由到不同的控制器/视图集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆