Note that there are some explanatory texts on larger screens.

plurals
  1. POConfused as to which Prototype helper to use (updated)
    primarykey
    data
    text
    <p>This is a continuation of <a href="https://stackoverflow.com/questions/2397874/confused-as-to-which-prototype-helper-to-use">Confused as to which Prototype helper to use</a>. My code has been updated to reflect other user's suggestions:</p> <p>(model) message.rb:</p> <pre><code>class Message &lt; ActiveRecord::Base after_create :destroy_old_messages def old_messages messages = Message.all(:order =&gt; 'updated_at DESC') if messages.size &gt;= 24 return messages[24..-1] else return [] end end protected # works without protected def destroy_old_messages messages = Message.all(:order =&gt; 'updated_at DESC') messages[24..-1].each {|p| p.destroy } if messages.size &gt;= 24 end end </code></pre> <p>(view) index.html.erb:</p> <pre><code>&lt;div id="messages"&gt; &lt;%= render :partial =&gt; @messages %&gt; &lt;/div&gt; &lt;%= render :partial =&gt; "message_form" %&gt; </code></pre> <p>(view) _message.html.erb:</p> <pre><code>&lt;% div_for message do %&gt; &lt;%= h message.created_at.strftime("%X") %&gt; - &lt;%= h message.author %&gt;&lt;%= h message.message %&gt; &lt;% end %&gt; </code></pre> <p>(view) _message_form.html.erb:</p> <pre><code>&lt;% remote_form_for :message, :url =&gt; { :action =&gt; "create" }, :html =&gt; { :id =&gt; 'message_form'} do |f| %&gt; &lt;%= f.text_area :message, :size =&gt; "44x3" %&gt;&lt;br /&gt; &lt;%= submit_to_remote 'submit_btn', 'submit', :url =&gt; { :action =&gt; 'create' } %&gt;&lt;br /&gt; &lt;% end %&gt; </code></pre> <p>(view) create.rjs:</p> <pre><code>page.insert_html :top, :messages, :partial =&gt; @message page[@message].visual_effect :grow page[:message_form].reset flash[:notice] flash.discard # @old_messages.each do |m| # page.remove(m.id) # end </code></pre> <p>(controller) messages_controller.rb:</p> <pre><code>class MessagesController &lt; ApplicationController def index @messages = Message.all(:order =&gt; "created_at DESC") respond_to do |format| format.html format.js end end def new @message = Message.new respond_to do |format| format.html end end def create @message = Message.new(params[:message]) # @old_messages = Message.old_messages respond_to do |format| if @message.save flash[:notice] = 'message created.' format.html { redirect_to(messages_url) } format.js else format.html { render :action =&gt; "new" } end end end def update @message = Message.find(params[:id]) respond_to do |format| if @message.update_attributes(params[:message]) flash[:notice] = 'message updated.' format.html { redirect_to(messages_url) } format.js else format.html { render :action =&gt; "edit" } end end end def destroy @message = Message.find(params[:id]) @message.destroy respond_to do |format| format.html { redirect_to(messages_url) } format.js end end end </code></pre> <p>With the exception of the old_messages method in the model, all of the commented code were recommended changes from the <a href="https://stackoverflow.com/questions/2397874/confused-as-to-which-prototype-helper-to-use">previous post</a> to make this work. But as soon as I uncomment the last three lines from create.rjs and @old_messages = Message.old_messages from the controller, I can't even submit messages with the message_form partial. Can anyone see what's wrong here? I'm just trying to create a basic app to help further my understanding of rails and rjs. I would greatly appreciate any suggestions or corrections you have to share, thank you for reading my post.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload