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: <table> 2: 3: <tr> 4: <td class="extend_td_time"><%= pizza_comment_item.created_at.strftime('%B %d, %Y - %H:%M %p' ) %></
5: <td class="second_td_comment"><%= pizza_comment_item.comment %></td> 6: </tr> 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
<table>
<tr> <td class="extend_td_time"><%= pizza_comment_item.created_at.strftime('%B %d, %Y - %H:%M %p' ) %></
<td class="second_td_comment"><%= pizza_comment_item.comment %></td> </tr>
</table>
pizza_comment.html.erb
<h3>History</h3> <div id="show_comment_list"> <%= render :partial => "pizza_comment_item", :collection => @pizza_comments %> </div> <p></p>
show.html.erb
<div id="pizza_comment"> <%= render(:partial => "pizza_comment", :object => @pizza_comment)%>
</div>
<% 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 %>