0

I added a link_to method to the view to allow users to select messages from the inbox to delete. I am using the Mailboxer gem. The error I receive when adding the code is ActiveRecord::RecordNotFound Couldn't find Conversation without an ID. Error points to the controller on line @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'

Conversations controller:

  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
xps15z
  • 1,769
  • 1
  • 20
  • 38
  • That reads to me as if params[:id] is nil. If you look at the development log, do you see an id with a value in the params? – agmcleod Aug 08 '14 at 13:05
  • @agmcleod It seems the issue is it's calling on a specific conversation to delete when the page loads when it shouldn't. I have the link_to for the delete checkbox, so once a user selects the conversations they want deleted, they will click on the delete icon to erase them. It appears the action is being called on immediately which is why it is nil. – xps15z Aug 08 '14 at 13:08
  • can you include the stack trace? also, please clarify **when** you get the error. is it when clicking the link to trash the conversation? or like you said, when you add the code for the link? – jvnill Aug 08 '14 at 13:33
  • @jvnill The error is when I add the code for the link. Without the code the page works fine. – xps15z Aug 08 '14 at 13:46
  • Show your whole view code then please. Or if it's a partial, what the view is that loads that partial. – agmcleod Aug 08 '14 at 13:50

1 Answers1

0

The error is caused by the combination of these lines.

# in the controller
helper_method :mailbox, :conversation

# in the view
= link_to 'Move to trash', [:trash, conversation], method: :post 

What happens is that the conversation variable called in the view is the controller method. Since you're in the index page, you don't have a params[:id] which causes find to raise the exception.

The easiest solution would be to remove conversation as a helper and just use @conversation in the view.

jvnill
  • 29,479
  • 4
  • 83
  • 86