Ruby-on-rails Rails 未定义的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25599151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails undefined method
提问by daisy
Why is this undefined? Does it have something to do with the @current_user?
为什么这是未定义的?和那个有关系@current_user吗?
I'm trying to create tasks for my challenges. And the created task should get /achievements. However, I get a GET 500 error.
我正在尝试为我的挑战创建任务。并且创建的任务应该得到/achievements。但是,我收到 GET 500 错误。
This is the error I get:
这是我得到的错误:
NoMethodError at /achievements
==============================
> undefined method `achievements' for #<User:0x00000105140dd8>
app/controllers/achievements_controller.rb, line 5
--------------------------------------------------
``` ruby
1 class AchievementsController < ApplicationController
2
3
4 def index
> 5 @achievements = @current_user.achievements
6 render :json => @achievements
7 end
8
9 def new 10 @achievement = Achievement.new
This is my code in my controller
这是我的控制器中的代码
class AchievementsController < ApplicationController
def index
@achievements = @current_user.achievements
render :json => @achievements
end
def new
@achievement = Achievement.new
render :json => @achievement
end
#create a new achievment and add it to the current user
#check then set the acheivments pub challenge id to the current pub challenge
def create
@achievement = Achievement.new achievement_params
@achievement.user = @current_user.id
@achievement.pub_challenge = params[:id]
if @achievement.save
# render :json => @achievement #{ status: 'ok'}
else
render :json => {:errors => @achievement.errors}
end
end
def show
@achievement = Achievement.find params[:id]
render :json => @achievement
end
def destroy
@achievement = Achievement.find params[:id]
@achievement.destroy
end
private
def achievement_params
params.require(:achievement).permit(:pub_challenges)
end
end
回答by lynt
You are missing the has_many :achievementsrelation in your User model.
您缺少has_many :achievementsUser 模型中的关系。
回答by Richard Peck
You'll need to create the ActiveRecord associationsyou require:
您需要创建所需的ActiveRecord 关联:
#app/models/user.rb
class User < ActiveRecord::Base
has_many :achievements
end
#app/models/achievement.rb
class Achievement < ActiveRecord::Base
belongs_to :user
end
This will give you the ability to call the achievementsmethod on any Userobjects you have.
这将使您能够achievements在User您拥有的任何对象上调用该方法。
Error
错误
The error you have is described as such:
您遇到的错误描述如下:
undefined method `achievements' for #<User:0x00000105140dd8>
This basically means that you're trying to call an undefined method on a Userobject. Might sound simple, but really, most people don't understand it.
这基本上意味着您试图在User对象上调用未定义的方法。听起来很简单,但实际上,大多数人并不理解。
To explain properly, you have to remember that Rails, by virtue of being built on Ruby is object orientated. This means that everythingyou do in Rails should be structured around objects - which are defined in your Models:
为了正确解释,您必须记住 Rails 是基于 Ruby 构建的,因此是面向对象的。这意味着你在 Rails中所做的一切都应该围绕对象来构建——这些对象在你的Models:


This means that each time you call an object, you're actually above to invoke a series of "methods" which will give you the ability to either manipulate the object itself, or any of the associated functionality it has.
这意味着每次调用object 时,您实际上都在调用一系列“方法”,这将使您能够操作对象本身或其具有的任何相关功能。
The problem you have is that your Userobject doesn't have the achievementsmethod. Whilst you could simply do the following to fix the issue, because it's Rails, you'll need to populate the record with associative data:
您遇到的问题是您的User对象没有该achievements方法。虽然您可以简单地执行以下操作来解决问题,因为它是 Rails,您需要使用关联数据填充记录:
#app/models/user.rb
class User < ActiveRecord::Base
has_many :achievements #-> what you need
def achievements
#this will also fix the error you see, although it's fundamentally incorrect
end
end
回答by Greg
Something that helped me with this type of error was that the database table was missing the relevant column. Adding the required column to the database fixed the issue.
帮助我解决此类错误的是数据库表缺少相关列。将所需的列添加到数据库修复了该问题。

