2016-12-14 40 views
1

我有一個簡單的軌道5應用與裝置和後每當我嘗試註冊,我得到以下錯誤:Rails的5:NoMethodError:未定義的方法`幫手」的MyMailer

NoMethodError in Devise::RegistrationsController#create undefined method `helper' for MyMailer(Table doesn't exist):Class

在2號線出現的錯誤:

class MyMailer < ApplicationRecord 
    helper :application # gives access to all helpers defined within `application_helper`. 
    include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url` 
    default template_path: 'devise/mailer' # to make sure that your mailer uses the devise views 
end 

你知道爲什麼這個類無法找到我的應用程序助手嗎?

+0

你爲什麼不用'include ApplicationHelper'? – dp7

回答

2

,如果它確實是一個郵遞員,而不是一個型號,你應該從ApplicationMailer繼承,而不是ApplicationRecord,否則它會在你的數據庫中尋找表來支持它。

class MyMailer < ApplicationMailer 
    ..... 
end 
1

對於您在Rails應用程序中使用的每個模型,都應該存在一個以模型名稱的複數形式命名的表。所以在你的情況下,因爲你的模型的名字是:MyMailer所以你應該創建一個名爲:my_mailers的表。

rails g migration create_my_mailers 
+0

現在告訴我:''method_missing':MyMailer的未定義方法'helper'(調用'MyMailer.connection'來建立連接):Class(NoMethodError)'我應該在哪裏做? – jonhue

+0

你爲什麼寫:'helper:application'?你想從'ApplicationHelper'的方法,寫'include ApplicationHelper'。 –

1

錯誤是因爲您在您的郵件程序中調用助手。如果你想在你的郵件程序中包含應用程序助手或任何助手,你必須使用「包含」關鍵字。

class MyMailer < ApplicationRecord 
    helper :application # This line is causing the error 
    include Devise::Controllers::UrlHelpers 
    default template_path: 'devise/mailer' 
end 

這是你應該如何包含您的應用助手

class MyMailer < ApplicationRecord 
    include ApplicationHelper 
    include Devise::Controllers::UrlHelpers 
    default template_path: 'devise/mailer' 
end 
相關問題