UsersController 中没有方法错误

发布于 2024-11-16 21:01:06 字数 767 浏览 5 评论 0原文

几周前我开始在 Rails 中编码,但我不明白为什么会出现这个错误。我使用 Devise 进行登录,使用 Formtastic 进行表单。在我添加 Acts_like_tags_on 并重置数据库之前,该应用程序一直正常工作。

错误信息: UsersController#show 中的 NoMethodError

undefined method `username' for nil:NilClass

app/controllers/users_controller.rb:19:in `show'
Request

Parameters:
{"id"=>"sign_in"}

这是我在用户控制器中的内容:

def show
 @user = User.find_by_username(params[:id])
 @title = @user.username

 respond_to do |format|
   format.html # show.html.erb
   format.json { render json: @user }
 end
end

任何输入都会有帮助。谢谢!

将 @user = User.find_by_username(params[:id]) 编辑为: @user = User.find_by_user(params[:id])

错误变为: undefined method `find_by_user' for #

用户名列确实存在于用户表中。

I began coding in Rails several weeks ago, and I can't figure out why I have this error. I'm using Devise for log-ins and Formtastic for forms. The app was working correctly until I added the Acts_like_tags_on and reset the database.

The error message:
NoMethodError in UsersController#show

undefined method `username' for nil:NilClass

app/controllers/users_controller.rb:19:in `show'
Request

Parameters:
{"id"=>"sign_in"}

This is what I have in the Users Controller:

def show
 @user = User.find_by_username(params[:id])
 @title = @user.username

 respond_to do |format|
   format.html # show.html.erb
   format.json { render json: @user }
 end
end

Any input would be helpful. Thanks!

After editing @user = User.find_by_username(params[:id]) to:
@user = User.find_by_user(params[:id])

The error becomes:
undefined method `find_by_user' for #

The username column does exist in the User table.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

紫轩蝶泪 2024-11-23 21:01:06
@user = User.find_by_username(params[:id])

上面那行可能返回零。如果你执行 User.find_by_username("Superman-is-awesome") 并且该用户名不存在于你的数据库中,它将返回 nil。

然后它试图做:

@title = @user.username

这本质上是:

@title = nil.username

这当然行不通。因此,您传入的参数可能有问题。

另外,请确保您的用户表有一列名为“用户名”的列?确保你已经运行:

rake db:migrate

以及。

@user = User.find_by_username(params[:id])

That line above may be returning nil. If you do User.find_by_username("Superman-is-awesome") and that username does not exist in your database, it's going to return nil.

Then it is trying to do:

@title = @user.username

Which is essentially:

@title = nil.username

Which of course won't work. So could be something wrong with the parameter you are passing in.

Also, make sure your User table have a column called 'username'? Make sure you've run:

rake db:migrate

As well.

拥抱没勇气 2024-11-23 21:01:06

如果正确配置了路由,则应该在用户资源之前设计路由,如下所示:

devise_for :users
resources :users, except: "create"

If you configured the routes correctly, you should have devise routes BEFORE user resource, like this:

devise_for :users
resources :users, except: "create"
迷爱 2024-11-23 21:01:06

这实际上是一个路由问题

问题是 devise 期望您有一个路由将:

“/users/sign_in”转换为sessions#new,

但您的路由文件缺少该路由,因此调度程序与:

“ users/:id” 路由然后转到:
users#show with :id =>; 'sign_in'

(因此当它尝试查找 id 为“sign_in”的用户时会抛出错误)

您需要阅读 devise 的 README 文档(如果您本地没有,请谷歌) - 尤其是描述的部分如何将标准路由集添加到 config/routes.rb 然后执行它所说的操作。 :)

应该在那之后。

This is actually a routing problem

The problem is that devise expects you to have a route that will turn:

"/users/sign_in" into sessions#new

but your routes file is missing that route, and so the dispatcher is matching against the:

"users/:id" route which then goes to:
users#show with :id => 'sign_in'

(and hence throws an error when it tries to find a user with the id of "sign_in")

You need to read the README doc for devise (google if you don't have it locally) - especially the part that describes how to add the standard set of routes to config/routes.rb Then do whatever it says. :)

Should be right after that.

甜味拾荒者 2024-11-23 21:01:06

我发现 user_id 被赋予了“users”值。在我的routes.rb中注释掉以下行后,id不再被赋予该值。

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

我还需要 users_controllers #show 具有以下行,因为我的用户路径使用用户名。删除“_by_username”会导致调用用户名的页面出现错误:

@user = User.find_by_username(params[:id])

I found that the user_id was being given the value of 'users'. After commenting the following line out in my routes.rb, the id was no longer given that value.

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

I also needed the users_controllers #show to have the following line, since my user path uses the username. removing '_by_username' caused an error on pages that called for the username:

@user = User.find_by_username(params[:id])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文