3

私はdevise gemを使用して、すべてのサインアップとサインインを処理しています。しかし、アプリケーションにユーザー プロファイルも追加したいので、show アクションのみでユーザー コントローラーを生成しました。get 'users/:id' => 'users#show'次に、routes.rbに追加しました。実際、/users/1 と入力すると機能しますが、ルートに名前を付ける方法が見つかりません。私が望むのは、show_user_path や user_path のようなものを取得して、特定のユーザーのコンテンツにリンクし、そのユーザーのプロファイルを表示できるようにすることです。

ここに私のroutes.rbがあります

Pinteresting::Application.routes.draw do
  resources :pins
  get 'users/:id' => 'users#show'
  devise_for :users 

  root "pins#index"
  get "about" => "pages#about"

そして、これで取得したルートは次のとおりです(show_user_pathのようなものであると予想されるものを強調表示しました):

pins_path                        GET     /pins(.:format)     pins#index
                                 POST    /pins(.:format)     pins#create
new_pin_path                     GET     /pins/new(.:format)     pins#new
edit_pin_path                    GET     /pins/:id/edit(.:format)    pins#edit
pin_path                         GET     /pins/:id(.:format)     pins#show
                                 PATCH   /pins/:id(.:format)     pins#update
                                 PUT     /pins/:id(.:format)     pins#update
                                 DELETE  /pins/:id(.:format)     pins#destroy
#this is the one I want a path!  GET     /users/:id(.:format)    users#show
new_user_session_path        GET     /users/sign_in(.:format)    devise/sessions#new
user_session_path                        POST    /users/sign_in(.:format)    devise/sessions#create
destroy_user_session_path        DELETE  /users/sign_out(.:format)   devise/sessions#destroy
user_password_path               POST    /users/password(.:format)   devise/passwords#create
new_user_password_path       GET     /users/password/new(.:format)       devise/passwords#new
edit_user_password_path      GET     /users/password/edit(.:format)      devise/passwords#edit
                                 PATCH   /users/password(.:format)   devise/passwords#update
                                 PUT     /users/password(.:format)   devise/passwords#update
cancel_user_registration_path    GET     /users/cancel(.:format)     devise/registrations#cancel
user_registration_path       POST    /users(.:format)    devise/registrations#create
new_user_registration_path       GET     /users/sign_up(.:format)    devise/registrations#new
edit_user_registration_path      GET     /users/edit(.:format)   devise/registrations#edit
                                 PATCH   /users(.:format)    devise/registrations#update
                                 PUT     /users(.:format)    devise/registrations#update
                                 DELETE  /users(.:format)    devise/registrations#destroy
root_path                        GET     /   pins#index
about_path                       GET     /about(.:format)    pages#about
4

3 に答える 3

6

devise の場合User、リソースではなく、単なるスコープです。デバイスが気にするのは認証です。

パスは の下にネストされていますが/user、定義されているリソースは実際にはセッション、登録、パスワードなどであることがわかります。

resources :usersルートを追加してUsersController(およびビュー) を作成するだけです。

ユーザー用のすべてのリソースを作成せずに、 を使用できるようにする場合は、次のようにオプションを使用してそのルートに名前を付けることができuser_path(user)ます。get 'users/:idas

get 'users/:id' => 'users#show', as: user
于 2013-10-17T19:40:13.570 に答える
1

上記の答えは素晴らしいですが、別のコントローラーを作成したくないが、たとえば、Devise::RegistrationsController から継承した登録コントローラーにアクションを追加したい場合は、ここで注意する価値があると思います。デバイススコープブロックを使用する必要があります:

devise_scope :user do
  get 'users/:id' => 'registrations#show', as: user
end
于 2014-10-28T18:03:53.697 に答える
0

答えは次のようになります。

get 'user/:id' => 'users#show', as: :user
于 2016-03-30T08:13:44.143 に答える