0

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

1 回答 1

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在视图中使用。

于 2014-08-08T13:50:51.610 回答