【问题标题】:Undefined method in ActionMailer class with ApplicationHelper带有 ApplicationHelper 的 ActionMailer 类中的未定义方法
【发布时间】:2014-12-16 03:17:59
【问题描述】:

application_helper.rb

class ApplicationHelper

  def user_name(user)
    if user.name.length > 2
      return user.name
    else
      return user.email
    end
  end

  ...

end

类 ForumMailer
class ForumMailer < ActionMailer::Base
  add_template_helper(ApplicationHelper)

  @username = user_name(User.find_by_id(1))
end

错误

NoMethodError: 未定义方法user_name 用于#

【问题讨论】:

    标签: ruby-on-rails actionmailer


    【解决方案1】:

    您需要添加helper :application

    class ForumMailer < ActionMailer::Base
    helper :application # include application helper
    
    .....
    end
    

    【讨论】:

      【解决方案2】:

      我最好的客人是你还没有包含你的 application_helper.rb。 尝试在你的 forum_mailer.rb 文件的第一行 require 助手:

      require './path/to/application_helper'
      

      【讨论】:

        【解决方案3】:

        您需要在实例方法中使用帮助器,而不是在类中。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-03-18
          • 2012-06-17
          相关资源
          最近更新 更多