Rails 3:文件夹的未定义方法消息
我正在运行 Rails 3 并且喜欢有一个消息系统。这是它的教程: http://www.novawave.net/public/rails_messaging_tutorial.html< /a>
它适用于 Rails 2,所以我尝试在 Rails 3 中实现它。
一切都很顺利,我可以发送消息。 但是,当我想检查我的收件箱时,会出现此错误:
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
当我使用以下命令检查控制台时:
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 时,它会返回错误。
当我尝试获取消息手册时,它正在工作:
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">]
模型和模型控制器与教程中的相同。
有什么想法吗?
问候
编辑:
*添加了错误和视图
错误:
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 %>
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
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>
Mailbox Controller:
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
everything is fine and the output is:
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">
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.
Any ideas?
Greets
EDIT:
*Added the Error and the View
Error:
undefined method `messages' for #<Folder:0x0000010409c900>
app/controllers/mailbox_controller.rb:11:in `show'
app/controllers/mailbox_controller.rb:5:in `index'
Mailbox/show view:
<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 %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能错过了在模型中建立关系的步骤。
检查您的Folder.rb(文件夹模型)是否与消息建立了has_many 关系。
您在控制器中遇到的错误告诉您情况并非如此,文件夹模型没有名为“messages”的方法,ActiceRecord(Rails 的 ORM)将使用 has_many 代码为您生成该方法。
伊恩。
You may have missed a step in establishing the relationships in your models.
Check that your Folder.rb (folder model) has a has_many relationship established with messages.
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.