我正在构建自定义收件箱,在我添加此行以显示要显示的所有邮件后,“<%user.received_messages.each do | message |%>”我现在得到一个“未定义的局部变量或方法'用户'”错误。不确定为什么额外的编码行导致了这个错误以及如何修复它。
_inbox.html:
<h2>Your Inbox</h2>
<% user.received_messages.each do |message| %>
No messages in your Inbox
<% end %>
<%= form_tag delete_multiple_user_messages_path, :method=>:post do %>
<table class="table table-bordered">
<tr>
<th>Delete?</th>
<th>Sent</th>
<th>Sender</th>
<th>Sent</th>
</tr>
<% for message in @messages %>
<tr>
<td><%= check_box_tag "delete[]", message.id %></td>
<td>
<% if message.read? %>
<%= link_to h(message.subject), user_message_path(@user, message) %>
<% else %>
<%= link_to "#{h(message.subject)} (unread)", user_message_path(@user, message) %>
<% end %>
</td>
<td><%= "#{message.sender.first_name} #{message.sender.last_name}" %></td>
<td><%=h message.created_at.to_s(:long) %></td>
</tr>
<% end %>
</table>
<%= submit_tag "Delete selected" %> |
<% end %>
<%= link_to "Compose New Message", new_user_message_path(@user)%> |
<%= link_to "View Sent Messages", user_messages_path(current_user, :mailbox => :sent)%>
的index.html:
<% if params[:mailbox] == "sent" %>
<%= render :partial => "sent" %>
<% elsif params[:mailbox] == "inbox" %>
<%= render :partial => "inbox" %>
<% elsif params[:mailbox] == "archieved" %>
<%= render :partial => "inbox" %>
<% end %>
messages_controller:
class MessagesController < ApplicationController
before_filter :set_user
def index
if params[:mailbox] == "sent"
@messages = @user.sent_messages
elsif params[:mailbox] == "inbox"
@messages = @user.received_messages
#elsif params[:mailbox] == "archieved"
# @messages = @user.archived_messages
end
end
def new
@message = Message.new
if params[:reply_to]
@reply_to = User.find_by_user_id(params[:reply_to])
unless @reply_to.nil?
@message.recepient_id = @reply_to.user_id
end
end
end
def create
@message = Message.new(params[:message])
@message.sender_id = @user_id
if @message.save
flash[:notice] = "Message has been sent"
redirect_to user_messages_path(current_user, :mailbox=>:inbox)
else
render :action => :new
end
end
def show
@message = Message.readingmessage(params[:id],@user_id)
end
def delete_multiple
if params[:delete]
params[:delete].each { |id|
@message = Message.find(id)
@message.mark_message_deleted(@message.id,@user_id) unless @message.nil?
}
flash[:notice] = "Messages deleted"
end
redirect_to user_messages_path(@user, @messages)
end
private
def set_user
@user = current_user
end
end
答案 0 :(得分:2)
那是因为你没有名为user
的辅助方法。也许您在current_user
控制器中有@user
或实例变量Messages
?
<强>更新强>
也许您希望将user
变量传递给部分?然后,您需要将render
调用更改为:<%= render :partial => "inbox", :user => some_user_instance %>