Professional Documents
Culture Documents
Controller - Rails Undefined Method - Stack Overflow
Controller - Rails Undefined Method - Stack Overflow
Controller - Rails Undefined Method - Stack Overflow
4 I'm trying to create tasks for my challenges. And the created task should get /achievements.
However, I get a GET 500 error.
2
NoMethodError at /achievements
==============================
app/controllers/achievements_controller.rb, line 5
--------------------------------------------------
``` ruby
4 def index
7 end
def index
@achievements = @current_user.achievements
end
def new
@achievement = Achievement.new
end
#check then set the acheivments pub challenge id to the current pub challenge
def create
@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
end
end
def show
end
def destroy
@achievement.destroy
end
private
def achievement_params
params.require(:achievement).permit(:pub_challenges)
end
end
Share Follow edited Dec 13, 2016 at 15:31 asked Sep 1, 2014 at 5:08
user6269864 daisy
51 1 1 3
Sorted by:
3 Answers
Highest score (default)
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
has_many :achievements
end
#app/models/achievement.rb
belongs_to :user
end
This will give you the ability to call the achievements method on any User objects you have.
Error
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
def achievements
#this will also fix the error you see, although it's fundamentally
incorrect
end
end
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.
Join Stack Overflow to find the best answer to your technical question, help others
Sign up
answer theirs.