2014-11-16 3 views
0

Я только что обновился до Mailboxer 0.12.4 и выполнил инструкции в Github Readme. У меня было два контроллера для работы с GemОбновление проблемы с Mailboxer 0.11.0 до 0.12.4

Уведомления

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 :: Уведомление

код, который выделен в

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

Я попытался создать объект уведомлений в консоли, и я получаю ту же ошибку. Я прочитал, что обновление изменило пространство имен, но я не знаю, как изменить свой код для учетной записи.

This is the closest I have found to a solution but the guy doesn't say how he fixed it

ответ

1

Ok Я получил эту работу, мне нужно, чтобы выяснить, почему, но это, кажется, связано с пространствами имен, которые были введены в обновление до 0.12.4.

Шаг 1: Изменить мои контроллеры

mailboxer_notification_controller.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 

Примечание:, что имена Param нужно изменить

mailboxer_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: Anywhere I Доступа метода, принадлежащий тезисы контроллеров, необходимых для обновления с правильными именами

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

    .... 
end 

Шаг 3: Обновления конфигурации/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 

Я не уверен, какие именно причины этих исправлений работают, мне нужно больше времени читать про пространства имен в рельсах. Если у кого есть хорошее объяснение, не стесняйтесь добавить к ответу

Смежные вопросы