0

我刚刚更新到 Mailboxer 0.12.4 并按照 Github 自述文件中的说明进行操作。我有两个控制器用于使用 Gem

通知

class NotificationsController < ApplicationController
    before_action :signed_in_user, only: [:create]

    def new
      @user = User.find(:user)
      @message = current_user.messages.new
    end

   def create
      @recepient = User.find(params[:notification][:id])

      current_user.send_message(@recepient, params[:notification][:body],params[:notification][:subject])
      flash[:success] = "Message has been sent!"

      redirect_to user_path @recepient
   end
end

对话

class ConversationsController < ApplicationController
    before_action :signed_in_user

    def index
       @conversations = current_user.mailbox.conversations.paginate(page: params[:page],:per_page => 5)
    end

    def show
        @conversation = current_user.mailbox.conversations.find_by( :id => params[:id] )

        @receipts = @conversation.receipts_for(current_user).reverse!
    end
end

我的用户模型有 act_as_messagable。更新后,我的用户控制器中的此方法会引发错误。

未初始化的常量 UsersController::Notification

突出显示的代码是

def show
    @user = User.find(params[:id])
    @message = Notification.new << this line
    ....
end

我尝试在控制台中创建一个 Notification 对象,但我得到了同样的错误。我已经读到更新已经改变了命名空间,但我不知道如何改变我的代码来解决这个问题。

这是我找到的最接近解决方案的方法,但那个人没有说他是如何解决的

4

1 回答 1

1

好的,我得到了这个工作,我需要弄清楚为什么,但它似乎与升级到 0.12.4 时引入的命名空间有关。

第 1 步:将我的控制器更改为

邮箱通知控制器.rb

class MailboxerNotificationsController < ApplicationController
    before_action :signed_in_user, only: [:create]

    def new
        @user = User.find(:user)
        @message = current_user.messages.new
    end

    def create
       @recepient = User.find(params[:mailboxer_notification][:id])

       current_user.send_message(@recepient, params[:mailboxer_notification][:body],params[:mailboxer_notification][:subject])
       flash[:success] = "Message has been sent!"

       redirect_to user_path @recepient
   end
end

注意:参数名称需要更改

邮箱_conversations_controller.rb

class MailboxerConversationsController < ApplicationController
    before_action :signed_in_user

    def index
        @conversations = current_user.mailbox.conversations.paginate(page: params[:page],:per_page => 5)
    end

    def show
        @conversation = current_user.mailbox.conversations.find_by( :id => params[:id] )

        @receipts = @conversation.receipts_for(current_user).reverse!
    end
end

第 2 步:在我访问属于这些控制器的方法的任何地方都需要使用正确的命名空间进行更新

def show
    @user = User.find(params[:id])
    @message = Mailboxer::Notification.new

    ....
end

第 3 步:更新您的 config/routes.rb

SampleApp::Application.routes.draw do
    resources :mailboxer_conversations
    resources :mailboxer_notifications, only: [:create]

    match '/sendMessage',   to: 'mailboxer_notifications#create',   via: 'post'

    match '/conversations', to: 'mailboxer_conversations#index',    via: 'get'
    match '/conversation',  to: 'mailboxer_conversations#show',     via: 'get'
    ....
end

我不确定这些修复工作的确切原因,我需要花更多时间阅读有关 Rails 中的命名空间的信息。如果有人有很好的解释,请随时添加到答案中

于 2014-11-18T21:17:26.277 回答