keaja
April 25, 2008, 8:48pm
1
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: <%=
pizza_comment_item.created_at.strftime(’%B %d, %Y - %H:%M %p’ ) %></
td>
5:
<%= pizza_comment_item.comment
%>
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' ) %>
<%= 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 %>
keaja
April 25, 2008, 10:11pm
2
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