Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 664133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:33:58+00:00 2026-05-13T23:33:58+00:00

This is a continuation of Confused as to which Prototype helper to use .

  • 0

This is a continuation of 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:33:59+00:00Added an answer on May 13, 2026 at 11:33 pm

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a continuation to this question which Oleg has answered. I have 2
This is in continuation to my previous question which was closed cause of similarity
This is continuation to the question I already asked a while back. I've been
This is a continuation of my question Python2.5.2 The code i developed is working
This is somewhat a continuation of this post I've taken the following code and
This is a continuation of pyparsing, Each, results name . I've been having great
I get this error when I run my code. This is a continuation from
This a continuation of this question. I have an Address class which contains basic
This is a continuation of this question: Original Question (SO) The answer to this
This is a continuation of the question posted in: How to load a jar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.