简体   繁体   English

Ruby on Rails 4:使用用户控制器进行设计,找不到具有'id'= sign_up的用户

[英]Ruby on Rails 4: Devise with Users Controller, Couldn't find User with 'id'=sign_up

I'm getting this error: Couldn't find User with 'id'=sign_up when I'm trying to go to localhost:3000/users/sign_up in my URL browser. 我收到此错误:当我尝试在URL浏览器中转到localhost:3000 / users / sign_up时Couldn't find User with 'id'=sign_up 用户

I have a separate Users Controller that I generated... 我有一个生成的单独的用户控制器...

I did this: 我这样做:

rails g devise:controllers users
rails g controller Users show

I didn't touch the controllers that was produced by devise. 我没有碰到由devise生产的控制器。

In the regular users controller that I created, I have this: 在我创建的常规用户控制器中,我有以下内容:

class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
  end
end

In my routes: 在我的路线中:

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

My rake routes looks like this: 我的rake routes看起来像这样:

new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
                 user GET    /users/:id(.:format)           users#show

It makes sense why I'm getting an error because :id is sign_up in my URL, I just don't get what I should be doing to fix this. 为什么出现错误是有道理的,因为:id在我的URL中是sign_up,我只是不知道应该怎么做才能解决此问题。

And of course, it'll always give me an error for anything after localhost:3000/users/ because of the id in my show method in users controller 当然,由于localhost:3000 / users /之后的用户 ,我的show方法中的id总是会给我任何错误

You have a separate users_controller which doesn't belongs to devise so devise_scope is of no use there. 你有一个独立的users_controller不属于devise使devise_scope是没有用那里。 If you need to use any devise controller action then devise_scope is useful. 如果您需要使用任何devise_scope控制器操作,那么devise_scope很有用。 So just make your routes as this: 因此,只需按照以下步骤进行路线:

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

The sequence of routes matters when we have similar routes. 当我们有相似的路线时,路线的顺序很重要。 In devise the routes sign_up , sign_out are defined as /users/sign_up(.:format) so suppose you visit sign_up url then it would check with the routes in devise and will find the match there. 在设计路由中, sign_upsign_out被定义为/users/sign_up(.:format)因此,假设您访问sign_up url,则它将检查设计中的路由并在其中找到匹配项。 So it will redirect you to that route. 因此它将重定向您到该路由。 Now if you enter /users/1 it will find in devise routes but it will find no match so ultimately it will map to your route and if there is no route defined as such it will throw error.. 现在,如果您输入/users/1 ,它将在设计路线中找到但找不到匹配项,因此最终它将映射到您的路线,并且如果没有这样定义的路线,则会引发错误。

Glad this helped. 很高兴这有所帮助。 :) :)

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

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