# Ajax Sort of Working. Model being saved but view not being updated

**URL:** <https://www.ruby-forum.com/t/ajax-sort-of-working-model-being-saved-but-view-not-being-updated/135873>\
**Category:** Rails\
**Created:** [April 25, 2008, 8:48pm UTC](https://www.ruby-forum.com/t/ajax-sort-of-working-model-being-saved-but-view-not-being-updated/135873 "2008-04-25T20:48:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![keaja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/838e76/32.png) [@keaja](https://www.ruby-forum.com/u/keaja)\
**Post date:** [April 25, 2008, 8:48pm UTC](https://www.ruby-forum.com/t/ajax-sort-of-working-model-being-saved-but-view-not-being-updated/135873/1 "2008-04-25T20:48:52Z")

</div>

I am trying to create a model and update the page element with ajax.  
The model is being saved and committed to the database. However the  
view just sits there and does nothing. If I refresh the page manually,  
the new model is there.

The error log:

ActionView::TemplateError (You have a nil object when you didn’t  
expect it!  
The error occurred while evaluating nil.created\_at) on line #4 of  
pizzas/\_pizza\_comment\_item.html.erb:  
1:

  
2:  
3:   
4:   
6:   
7:

Controller:

def show  
@user\_id = session[:user]  
@user = User.find(@user\_id)  
@pizza = Pizza.find(params[:id], :conditions =\> {:user\_id =\>  
@user\_id})  
@pizza\_comments = PizzaComment.find(:all, :conditions =\> {:pizza\_id  
=\> @pizza, :user\_id =\> @user\_id}, :order =\> “created\_at DESC”)  
end

def add\_pizza\_comment  
@user\_id = session[:user]  
@user = User.find(@user\_id)

@pizza\_comment = PizzaComment.create!(params[:pizza\_comment])  
render :update do |page|  
page.replace\_html(“pizza\_comment”, :partial =\>  
“pizza\_comment”, :object =\> @pizza\_comment)  
end  
end

\_pizza\_comment\_item.html.erb

| \<%=  
pizza\_comment\_item.created\_at.strftime(’%B %d, %Y - %H:%M %p’ ) %\>\</  
td\>  
5: | \<%= pizza\_comment\_item.comment  
%\> |

| \<%= pizza\_comment\_item.created\_at.strftime('%B %d, %Y - %H:%M %p' ) %\> | \<%= pizza\_comment\_item.comment %\> |

pizza\_comment.html.erb

### History

 \<%= render :partial =\> "pizza\_comment\_item", :collection =\> @pizza\_comments %\>

show.html.erb

 \<%= render(:partial =\> "pizza\_comment", :object =\> @pizza\_comment)%\> 

\<% form\_remote\_tag :url =\> { :action =\> ‘add\_pizza\_comment’ } do %\>  
\<%= text\_field ‘pizza\_comment’, ‘comment’ %\>  
\<%= hidden\_field ‘pizza\_comment’, ‘user\_id’, :value =\> “1” %\>  
\<%= hidden\_field ‘pizza\_comment’, ‘pizza\_id’, :value =\> “2”%\>  
\<%= submit\_tag ‘Add Comment’ %\>

\<% end %\>

---

<div class="post-metadata">

**Author:** ![keaja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/838e76/32.png) [@keaja](https://www.ruby-forum.com/u/keaja)\
**Post date:** [April 25, 2008, 10:11pm UTC](https://www.ruby-forum.com/t/ajax-sort-of-working-model-being-saved-but-view-not-being-updated/135873/2 "2008-04-25T22:11:55Z")

</div>

On 25 Apr 2008, at 19:47, Keaja wrote:

> ### History
> 
> \<%= render :partial =\> "pizza\_comment\_item", :collection =\> @pizza\_comments %\>

Your add\_pizza\_comment action renders the pizza\_comment partial, which  
references @pizza\_comments. That action doesn’t defined @pizza\_comments

Fred
