简体   繁体   English

使用Rails 4进行设计:用户显示路线不存在

[英]Devise with Rails 4: user show route doesn't exist

I am using the devise gem to handle all the sign up and sign in stuff. 我正在使用devise gem处理所有注册和登录内容。 But I also want to add user profiles to my application, so I generated a user controller with only a show action. 但是我也想将用户配置文件添加到我的应用程序中,因此我生成了一个仅带有show操作的用户控制器。 Then I added get 'users/:id' => 'users#show' to routes.rb. 然后我将get'users get 'users/:id' => 'users#show'到route.rb。 In fact, typing /users/1 works, but I can't find a way to name the route. 实际上,键入/ users / 1可以,但是我找不到命名该路线的方法。 What I want is to get something like show_user_path or user_path so I can link to a given user's content and show that user's profile. 我想要的是获得诸如show_user_path或user_path之类的东西,以便我可以链接到给定用户的内容并显示该用户的个人资料。

Here is my routes.rb 这是我的routes.rb

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

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

And here are the routes I get with it (I highlighted the one I expect to be something like show_user_path): 这是我得到的路线(我强调了一条我希望类似于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

For devise, User is not the resource, it's just a scope. 出于设计目的, User不是资源,而只是范围。 What devise cares about is authentication. 设计关心的是身份验证。

Although the paths are nested under /user , you will notice that the resources that are defined are actually things like sessions, registrations, passwords... 尽管路径嵌套在/user ,您会注意到定义的资源实际上是会话,注册,密码等内容。

Just add resources :users in your routes and create a UsersController (and the views). 只需在您的路线中添加resources :users并创建一个UsersController (和视图)即可。

If you don't want to create all the resources for users and just be able to user user_path(user) with your get 'users/:id , you can name that route using the as option, like this: 如果您不想为用户创建所有资源,而只希望使用user_path(user) get 'users/:id用户user_path(user) ,则可以使用as选项命名该路由,如下所示:

get 'users/:id' => 'users#show', as: user

The answer above is great but I think it's worth noting here that if you don't want to create a different controller, but want to add an action to, say, your registrations controller that you inherit from the Devise::RegistrationsController, then you need to use the devise scope block : 上面的答案很好,但是我想在这里值得注意的是,如果您不想创建其他控制器,而是想向从Devise :: RegistrationsController继承的注册控制器中添加操作,那么您需要使用设计范围块

devise_scope :user do
  get 'users/:id' => 'registrations#show', as: user
end

答案应该是这样的:

get 'user/:id' => 'users#show', as: :user

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM