简体   繁体   中英

Mailboxer Gem using multiple models

I Have two models User and employee which need to interact with each other. This is a controller based on the sample app ( controller ). I need it to work for both models but adding an if/else statement for def like mailbox etc to determine if a employee or user is logged in displays an "stack level too deep" error. I'm new to rails and cannot understand what is my mistake after multiple googling/SO. The messaging should be between both these two models and the initiator should be the user. (Update: An SO post I found Mailboxer Gem--Restrict messaging ) Thanks a ton!

class ConversationsController < ApplicationController
  before_filter :authenticate_employee! or authenticate_user!
  helper_method :mailbox, :conversation

  def create
    recipient_emails = conversation_params(:recipients).split(',')
    recipients = User.where(email: recipient_emails).all
if(user_signed_in?)
    conversation = current_user.send_message(recipients, *conversation_params(:body, :subject)).conversation
else
  conversation = current_employee.send_message(recipients, *conversation_params(:body, :subject)).conversation
end
    redirect_to conversation_path(conversation)

end

  def reply
    current_user.reply_to_conversation(conversation, *message_params(:body, :subject))
    redirect_to conversation_path(conversation)
  end

  def trash
    conversation.move_to_trash(current_user)
    redirect_to :conversations
  end

  def untrash
    conversation.untrash(current_user)
    redirect_to :conversations
  end

  private

  def mailbox
    if(user_signed_in?)
    @mailbox ||= current_user.mailbox
else
    @mailbox ||= current_employee.mailbox
end
    redirect_to conversation_path(conversation)  
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
end`

The "stack level too deep" error typically arises because of an infinite loop/recursion/redirect. Looking at your code above, I do not see any obvious culprit, but debugging using pry may help you track it down.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM