views:

65

answers:

2

This is a continuation of http://stackoverflow.com/questions/2397874/confused-as-to-which-prototype-helper-to-use. My code has been updated to reflect other user's suggestions:

(model) message.rb:

class Message < ActiveRecord::Base
  after_create :destroy_old_messages
  def old_messages
    messages = Message.all(:order => 'updated_at DESC')
    if messages.size >= 24
      return messages[24..-1]
    else
      return []
    end
  end

  protected # works without protected
  def destroy_old_messages
    messages = Message.all(:order => 'updated_at DESC')
    messages[24..-1].each {|p| p.destroy } if messages.size >= 24
  end
end

(view) index.html.erb:

<div id="messages">
  <%= render :partial => @messages %>
</div>
<%= render :partial => "message_form" %>

(view) _message.html.erb:

<% div_for message do %>
  <%= h message.created_at.strftime("%X") %>  - <%= h message.author %><%= h message.message %>
<% end %>

(view) _message_form.html.erb:

<% remote_form_for :message, :url => { :action => "create" }, :html => { :id => 'message_form'}  do |f| %>

  <%= f.text_area :message, :size => "44x3" %><br />
  <%= submit_to_remote 'submit_btn', 'submit', :url => { :action => 'create' } %><br />
<% end %>

(view) create.rjs:

page.insert_html :top, :messages, :partial => @message
page[@message].visual_effect :grow
page[:message_form].reset
flash[:notice]
flash.discard
# @old_messages.each do |m|
  # page.remove(m.id)
# end

(controller) messages_controller.rb:

class MessagesController < ApplicationController
  def index
    @messages = Message.all(:order => "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 => "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 => "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

With the exception of the old_messages method in the model, all of the commented code were recommended changes from the previous post 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.

A: 

it's not what you're asking for, but i have a suggestion...

to get the older messages you can use named_scope.

in your case, (if i understood what you want) i think it would be something like:

# model
named_scope :limit, lambda { |num| { :limit => num } }
named_scope :order, lambda { |ord| { :order => ord } }

and

#controller
Message.order("updated_at DESC").limit(24)
j.
A: 

the problem is that old_messages is an instance method, and you're calling from a class.

if you do

def self.old_messages
   # ...
end

it's now a class method.

this blog has a good explanation about class and instance methods.

j.
Thank you for your answer. The form now works again along with adding the messages via rjs/ajax. But the following in create.rjs doesn't seem to be doing what I expect it to: @old_messages.each do |m| page.remove(m.id) endAs messages are added to the list, I'd like the ones that are being removed by the model to also be removed via rjs in the view as message_form is submitted. A user in my previous post suggested that the above code would work. It looks correct to me. I've tried reordering my create.rjs but it doesn't change the behavior.
It seems that code isn't formatted in the comment boxes. If it's difficult to read, the troublesome code is the last three lines of create.rjs above (in it's uncommented form of course)
i'm not sure if i understand. when you add a new message and get redirected to the messages list, are the old ones still being shown?
j.