Controller - Rails Undefined Method - Stack Overflow

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

 

Rails undefined method


Asked
7 years, 9 months ago Modified
4 years, 6 months ago Viewed
26k times

Why is this undefined? Does it have something to do with the @current_user ?

4 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:

2
NoMethodError at /achievements

==============================

> undefined method `achievements' for #<User:0x00000105140dd8>

app/controllers/achievements_controller.rb, line 5

--------------------------------------------------

``` ruby

1 class AchievementsController < ApplicationController


2

4 def index

> 5 @achievements = @current_user.achievements

6 render :json => @achievements

7 end

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

Join Stack Overflow to find the best answer to your technical question, help others
# render :json => @achievement #{ status: 'ok'}
Sign up
answer theirs.
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

ruby-on-rails controller undefined

Share Follow edited Dec 13, 2016 at 15:31 asked Sep 1, 2014 at 5:08
user6269864 daisy
51 1 1 3

2 Please show us the relevant sections of your user model too.


– Taryn East
Sep 1, 2014 at 5:12

Sorted by:
3 Answers
Highest score (default)

¿No encuentras la respuesta? Pregunta en Stack Overflow en español.

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

9 Share Follow edited Sep 2, 2014 at 4:39 answered Sep 1, 2014 at 5:11
Brad Werth lynt
16.9k 9 62 86 136 1 5

Join Stack Overflow to find the best answer to your technical question, help others
Sign up
answerYou'll need to create the ActiveRecord associations you require:
theirs.
9 #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 achievements method on any User objects you have.

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 User object. Might
sound simple, but really, most people don't understand it.

To explain properly, you have to remember that Rails, by virtue of being built on Ruby is object
orientated. This means that everything you do in Rails should be structured around objects -
which are defined in your Models :

Join Stack Overflow to find the best answer to your technical question, help others
Sign up
answer theirs.
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.

The problem you have is that your User object doesn't have the achievements method. 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:

#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

Share Follow answered Sep 1, 2014 at 9:45


Richard Peck
74.8k 9 90 142

Join Stack Overflow to find the best answer to your technical question, help others
Sign up
answer theirs.
Something that helped me with this type of error was that the database table was missing the
0 relevant column. Adding the required column to the database fixed the issue.

Share Follow answered Dec 13, 2017 at 6:17


Greg
515 1 9 19

Join Stack Overflow to find the best answer to your technical question, help others
Sign up
answer theirs.

You might also like