Rails 3:文件夹的未定义方法消息 [英] Rails 3: undefined method messages for Folder

查看:92
本文介绍了Rails 3:文件夹的未定义方法消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行Rails 3,并且希望有一个消息系统.这是针对它的教程: http://www.novawave.net/public/rails_messaging_tutorial.html

I'm running Rails 3 and like to have a message system. Here is the tutorial for it: http://www.novawave.net/public/rails_messaging_tutorial.html

它用于Rails 2,所以我试图在Rails 3中实现它.

It's for Rails 2 so I was trying to implement it in Rails 3.

一切都很好,我可以发送消息. 但是,当我想检查收件箱时,会显示此错误:

Everything went fine and i can send messages. But when i like to check my inbox this error shows up:

undefined method `messages' for #<Folder:0x0000010419fd48>

邮箱控制器:

class MailboxController < ApplicationController
  def index
    redirect_to new_session_path and return unless logged_in?
    @folder = current_user.inbox
    show
    render :action => "show"
  end

  def show
    @folder ||= current_user.folders.find(params[:id])
    @messages = @folder.messages.paginate :per_page => 10, :page => params[:page], :include => :message, :order => "messages.created_at DESC"
  end
end

当我使用以下方法检查控制台时:

When I check the console with:

User.find(9).inbox

一切都很好,输出为:

ruby-1.9.2-p180 :085 > User.find(9).inbox
 => #<Folder id: 1, user_id: 9, parent_id: nil, name: "Inbox", created_at: "2011-04-27 21:37:00", updated_at: "2011-04-27 21:37:00"> 

但是当我添加.messages时,它会返回错误.

But when I add .messages it returns the error.

当我尝试获取消息手册时,它正在工作:

When i try to fetch the messages manual it's working:

User.find(9).received_messages
 => [#<MessageCopy id: 7, recipient_id: 9, message_id: 8, folder_id: 1, created_at: nil, updated_at: "2011-04-27 23:15:25">, #<MessageCopy id: 8, recipient_id: 9, message_id: 9, folder_id: 1, created_at: nil, updated_at: "2011-04-27 23:25:06">]

模型和控制器与教程中的相同.

The Models & Controllers are the same as in the tutorial.

有什么想法吗?

礼物

*添加了错误和视图

错误:

undefined method `messages' for #<Folder:0x0000010409c900>

app/controllers/mailbox_controller.rb:11:in `show'
app/controllers/mailbox_controller.rb:5:in `index'

邮箱/显示视图:

<h2><%=h @folder.name %></h2>

<table border="1">
  <tr>
    <th>From</th>
    <th>Subject</th>
    <th>Received</th>
  </tr>

  <% @messages.each do |message| %>
    <tr>
      <td><%=h message.author.login %></td>
      <td><%= link_to h(message.subject), message_path(message) %></td>
      <td><%= distance_of_time_in_words(message.created_at, Time.now) %> ago</td>
    </tr>
  <% end %>
</table>

<%= will_paginate @messages %>

推荐答案

您可能错过了在模型中建立关系的步骤.

You may have missed a step in establishing the relationships in your models.

检查您的Folder.rb(文件夹模型)是否具有与邮件建立的has_many关系.

Check that your Folder.rb (folder model) has a has_many relationship established with messages.

class Folder < ActiveRecord::Base
   has_many :messages, :class_name => "MessageCopy"

您在控制器中遇到的错误告诉您事实并非如此,文件夹模型没有称为消息"的方法,ActiceRecord(Rails'ORM)将使用has_many代码为您生成该方法.

The error you are getting in the controller is telling you that this is not the case, the folder model has no method called 'messages', which ActiceRecord (Rails' ORM) will generate for you with the has_many code.

ian.

这篇关于Rails 3:文件夹的未定义方法消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆