我在视图中添加了一个link_to方法,允许用户从收件箱中选择要删除的邮件。我正在使用Mailboxer gem。添加代码时收到的错误是ActiveRecord::RecordNotFound Couldn't find Conversation without an ID
。错误指向@conversation ||= mailbox.conversations.find(params[:id])
index.html.slim:
ul.right
li
= link_to 'Move to trash', [:trash, conversation], method: :post
img alt="" src="/assets/del_icon.png" /
li
input type="checkbox" class='select_conversations'
对话控制器:
helper_method :mailbox, :conversation
before_filter :conversation, only: :show
before_filter :check_has_access
def index
@user = current_user
sentbox_page = params[:page] if params[:sentbox].present?
inbox_page = params[:page] if params[:inbox].present?
mailbox = @user.mailbox
@inbox = mailbox.inbox.paginate(:page => inbox_page, :per_page => 5)
@sentbox = mailbox.sentbox.paginate(:page => sentbox_page, :per_page => 5)
render layout: 'new_application'
end
def show
user = current_user
@receipts = conversation.receipts_for(user).paginate(:page => params[:page], :per_page => 5)
@conversation.receipts.recipient(user).update_all(is_read: true)
respond_to do |format|
format.html {render layout: 'new_application'}
format.js {}
end
end
def reply
current_user.reply_to_conversation(conversation, *message_params(:body, :subject))
redirect_to conversation
end
def trash_folder
@trash ||= current_user.mailbox.trash.all
end
def trash
conversation.move_to_trash(current_user)
redirect_to :conversations
end
def untrash
conversation.untrash(current_user)
redirect_to :conversations
end
def empty_trash
current_user.mailbox.trash.each do |conversation|
conversation.receipts_for(current_user).update_all(:deleted => true)
end
redirect_to :conversations
end
private
def mailbox
@mailbox ||= current_user.mailbox
end
def conversation
@conversation ||= mailbox.conversations.find(params[:id])
end
def conversation_params(*keys)
fetch_params(:conversation, *keys)
end
def message_params(*keys)
fetch_params(:message, *keys)
end
def fetch_params(key, *subkeys)
params[key].instance_eval do
case subkeys.size
when 0 then self
when 1 then self[subkeys.first]
else subkeys.map{|k| self[k] }
end
end
end
protected
def check_has_access
redirect_to(root_url) unless Subscription.exists?(user_id: current_user.try(:id) || -1, cancelled: nil)
end
end
答案 0 :(得分:0)
错误是由这些行的组合引起的。
# in the controller
helper_method :mailbox, :conversation
# in the view
= link_to 'Move to trash', [:trash, conversation], method: :post
在视图中调用的conversation
变量是控制器方法。由于您位于索引页面中,因此您没有params[:id]
导致find
引发异常。
最简单的解决方案是删除conversation
作为帮助,然后在视图中使用@conversation
。