困惑于使用哪个原型助手(已更新)

发布于 2024-08-24 11:07:19 字数 3254 浏览 5 评论 0原文

这是对使用哪个原型助手感到困惑。我的代码已更新以反映其他用户的建议:(

模型)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

视图)index.html.erb:(

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

视图)_message.html.erb:(

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

视图)_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 %>

视图)创建。 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

控制器)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

除了模型中的 old_messages 方法之外,所有注释的代码均建议从 上一篇文章 来完成这项工作。但是,一旦我从控制器中取消注释 create.rjs 和 @old_messages = Message.old_messages 的最后三行,我什至无法使用 message_form 部分提交消息。谁能看出这里出了什么问题吗?我只是想创建一个基本的应用程序来帮助进一步了解 Rails 和 rjs。我非常感谢您分享的任何建议或更正,感谢您阅读我的文章。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红尘作伴 2024-08-31 11:07:19

这不是你要求的,但我有一个建议......

要获取旧消息,你可以使用 named_scope

就你而言,(如果我明白你想要什么)我认为它会是这样的:

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

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

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)
ι不睡觉的鱼゛ 2024-08-31 11:07:19

问题是 old_messages 是一个实例方法,并且您是从类中调用的。

如果你这样做,

def self.old_messages
   # ...
end

它现在是一个类方法。

此博客关于类和实例方法的很好的解释。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文