Keaja
2008-Apr-25 18:47 UTC
Ajax Sort of Working. Model being saved but view not being updated.
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'' ) %></ td> 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> <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 %> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Apr-25 20:10 UTC
Re: Ajax Sort of Working. Model being saved but view not being updated.
On 25 Apr 2008, at 19:47, Keaja wrote:> > 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. > > > 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.html.erb > > <h3>History</h3> > <div id="show_comment_list"> > <%= render :partial => "pizza_comment_item", :collection => > @pizza_comments %> > </div> > <p></p>Your add_pizza_comment action renders the pizza_comment partial, which references @pizza_comments. That action doesn''t defined @pizza_comments Fred --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---