【问题标题】:Mailboxer Gem, Admin ViewMailboxer Gem,管理员视图
【发布时间】:2014-02-07 14:18:27
【问题描述】:

我在我的应用程序中的用户模型之间使用邮箱 gem 进行对话/消息。这一切都很好,这要归功于堆栈溢出方面的一些巨大帮助。我现在正在尝试设置一个部分,以便管理员可以查看正在发生的所有对话。

我为对话创建了一个控制器和视图,嵌套在我的管理部分中。我已经在索引页面上提取了所有对话:

 def index
    @admin_conversations = Conversation.all
  end

这已按预期列出了所有对话以及显示每个对话的链接。

我遇到的问题是,邮箱 Gem 设置为仅允许 current_user 查看 current_user 参与的对话。所以我可以单击一些对话(以管理员身份签名)并查看内容,但有些(在其他测试用户之间)我看不到,即它抛出异常,例如:

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

如何在我的管理控制器中定义方法以便管理员可以看到所有内容?

我目前正在使用 cancan 并允许我拥有的所有 3 个用户角色(管理员、客户和供应商)如下:

can :manage, Conversation

...所以这不是正常的授权问题。

这是我的对话控制器:

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

答案可能很愚蠢,但我对此很陌生......

谢谢

【问题讨论】:

    标签: ruby-on-rails cancan mailboxer


    【解决方案1】:

    在你的对话方式中,你的电话mailbox.conversations.find(params[:id])

    mailbox.conversations 将您限制在当前用户的对话中。

    试试Conversation.find(params[:id])

    【讨论】:

    • 好的,谢谢,成功了。我按照您的建议在我的 admin:conversations 控制器中定义了它。我现在唯一需要做的就是更改显示路径以包含链接的“管理员”部分。目前它要去root/conversation/id,我需要root/admin/conversation/id
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多