gpt4 book ai didi

ruby-on-rails - Rails 未定义的方法

转载 作者:行者123 更新时间:2023-12-04 21:57:12 26 4
gpt4 key购买 nike

为什么这是未定义的?和@current_user有关系吗? ?

我正在尝试为我的挑战创建任务。并且创建的任务应该获得/成就。但是,我收到 GET 500 错误。

这是我得到的错误:

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

这是我在 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

最佳答案

您缺少 has_many :achievements用户模型中的关系。

关于ruby-on-rails - Rails 未定义的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25599151/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com