# NoMethodError - why?

**URL:** <https://www.ruby-forum.com/t/nomethoderror-why/58954>\
**Category:** Rails\
**Created:** [April 27, 2006, 12:30pm UTC](https://www.ruby-forum.com/t/nomethoderror-why/58954 "2006-04-27T12:30:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![robtotheb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/db5fbb/32.png) [@robtotheb](https://www.ruby-forum.com/u/robtotheb)\
**Post date:** [April 27, 2006, 12:30pm UTC](https://www.ruby-forum.com/t/nomethoderror-why/58954/1 "2006-04-27T12:30:22Z")

</div>

Getting this error when hitting update.

NoMethodError in Article#update

You have a nil object when you didn’t expect it!  
You might have expected an instance of ActiveRecord::Base.  
The error occured while evaluating nil.update\_attributes

Source code.

def create  
@article = Article.new(params[:article])  
@article.user = @session[‘user’]  
if @article.save  
flash[:notice] = ‘article was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

def edit  
@article = Article.find\_by\_article\_name(params[:article\_name])  
if @article.user != @session[‘user’]  
flash[:notice] = ‘You cannot edit an article you don’t own.’  
redirect\_to :action =\> ‘show’, :id =\> @article  
end  
end

def update  
@article = Article.find\_by\_article\_name(params[:article\_name])  
if @article.update\_attributes(params[:article])  
flash[:notice] = ‘Article was successfully updated.’  
redirect\_to :action =\> ‘show’, :id =\> @article  
else  
render :action =\> ‘edit’  
end  
end

---

<div class="post-metadata">

**Author:** ![robtotheb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/db5fbb/32.png) [@robtotheb](https://www.ruby-forum.com/u/robtotheb)\
**Post date:** [April 27, 2006, 12:58pm UTC](https://www.ruby-forum.com/t/nomethoderror-why/58954/2 "2006-04-27T12:58:56Z")

</div>

Rob B. wrote:

> Getting this error when hitting update.
> 
> NoMethodError in Article#update
> 
> You have a nil object when you didn’t expect it!  
> You might have expected an instance of ActiveRecord::Base.  
> The error occured while evaluating nil.update\_attributes
> 
> Source code.
> 
> def create  
> @article = Article.new(params[:article])  
> @article.user = @session[‘user’]  
> if @article.save  
> flash[:notice] = ‘article was successfully created.’  
> redirect\_to :action =\> ‘list’  
> else  
> render :action =\> ‘new’  
> end  
> end
> 
> def edit  
> @article = Article.find\_by\_article\_name(params[:article\_name])  
> if @article.user != @session[‘user’]  
> flash[:notice] = ‘You cannot edit an article you don’t own.’  
> redirect\_to :action =\> ‘show’, :id =\> @article  
> end  
> end
> 
> def update  
> @article = Article.find\_by\_article\_name(params[:article\_name])  
> if @article.update\_attributes(params[:article])  
> flash[:notice] = ‘Article was successfully updated.’  
> redirect\_to :action =\> ‘show’, :id =\> @article  
> else  
> render :action =\> ‘edit’  
> end  
> end

@article is nil, therefore find\_by\_article\_name didn’t find one. You  
can aboid the symtom by checking for nil before calling  
update\_attrbutes. As for why it didn’t find one, if you’re doing things  
the standard way, you probably want params[:article][:name]

Alan

---

<div class="post-metadata">

**Author:** ![robtotheb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/db5fbb/32.png) [@robtotheb](https://www.ruby-forum.com/u/robtotheb)\
**Post date:** [April 27, 2006, 1:00pm UTC](https://www.ruby-forum.com/t/nomethoderror-why/58954/3 "2006-04-27T13:00:05Z")

</div>

Alan F. wrote:

> @article is nil, therefore find\_by\_article\_name didn’t find one. You  
> can aboid the symtom by checking for nil before calling  
> update\_attrbutes. As for why it didn’t find one, if you’re doing things  
> the standard way, you probably want params[:article][:name]

Apologies for both the vast amount of quoted text, and the poor quality  
typing. It’s definitely lunchtime.
