Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails undefined method

Why is this undefined? Does it have something to do with the @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.

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
like image 347
daisy Avatar asked Dec 01 '22 15:12

daisy


1 Answers

You are missing the has_many :achievements relation in your User model.

like image 124
lynt Avatar answered Dec 04 '22 04:12

lynt