У меня есть две модели пользователя и сотрудника, которые должны взаимодействовать друг с другом. Это контроллер на основе примера приложения (controller< /а>). Мне нужно, чтобы он работал для обеих моделей, но добавление оператора if/else для определения, например почтового ящика и т. д., чтобы определить, вошел ли сотрудник или пользователь в систему, отображает ошибку «слишком глубокий уровень стека». Я новичок в рельсах и не могу понять, в чем моя ошибка после многократного поиска в Google/SO. Обмен сообщениями должен осуществляться между обеими этими двумя моделями, а инициатором должен быть пользователь. (Обновление: сообщение SO, которое я нашел Mailboxer Gem - Ограничить обмен сообщениями) Огромное спасибо!
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`