我在视图中添加了一个 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