Mailboxer Gem, Admin View

前提是你 提交于 2019-12-11 10:46:47

问题


I'm using the mailboxer gem for conversations/messages between the User model in my app. This is all working fine, thanks to some great help in stack overflow. I'm now trying to setup a section so admin can view all the conversations that are happening.

I have created a controller and view for conversations, nested in my admin section. I've pulled in all conversations on the index page with:

 def index
    @admin_conversations = Conversation.all
  end

This has listed all the conversations, and a link to show each conversation, as expected.

The problem I am having, is that the mailboxer Gem is setup to only allow the current_user to view conversations that the current_user is a participant in. So I can click some of the conversations, (signed is as an admin) and see the contents, but some (that are between other test users) I cannot see i.e it throws an exception such as:

Couldn't find Conversation with id=5 [WHERE "notifications"."type" = 'Message' AND "receipts"."receiver_id" = 35 AND "receipts"."receiver_type" = 'User']

How can I define the method in my admin controller so that the admin can see everything?

I'm currently using cancan and allowing all 3 user roles I have (admin, client and supplier) like this:

can :manage, Conversation

...so it isn't a normal authorisation problem.

Here is my conversations controller:

class ConversationsController < ApplicationController
  authorize_resource
  helper_method :mailbox, :conversation


  def create
    recipient_emails = conversation_params(:recipients).split(',')
    recipients = User.where(email: recipient_emails).all

    conversation = current_user.
      send_message(recipients, *conversation_params(:body, :subject)).conversation

    redirect_to :back, :notice => "Message Sent! You can view it in 'My Messages'."
  end

 def count
  current_user.mailbox.receipts.where({:is_read => false}).count(:id, :distinct => true).to_s
 end    

  def reply
    current_user.reply_to_conversation(conversation, *message_params(:body, :subject))
    redirect_to 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
    @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
end

The answer is probably something pretty silly, but I am new to this...

Thanks


回答1:


In your conversation method, your calling mailbox.conversations.find(params[:id])

mailbox.conversations is what is limiting you to the current user's conversations.

Try just Conversation.find(params[:id])



来源:https://stackoverflow.com/questions/21630044/mailboxer-gem-admin-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!