爲什麼這個未定義?它與@current_user
有什麼關係?Rails未定義方法
我正在嘗試爲我的挑戰創建任務。創建的任務應該獲得/成就。但是,我收到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
這是我的代碼在我的控制器
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
請向我們展示您的用戶模型的相關部分。 – 2014-09-01 05:12:13