首页 > 解决方案 > 无法自动加载常量 SetTitle /controllers/concerns/set_title.rb 来定义它

问题描述

我正在尝试设置页面标题。我的关注文件夹下有一个 set_title.rb 文件。

module SetTitle
extend ActiveSupport::Concern

included do
    before_action:set_name
end

def set_name
    if current_user
        {
            @set_title_name = current_user.name
        }
    else
        {
            @set_title_name = "Guest"
        }
    end
end

这是我的 application_controller.rb 文件

class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
include DeviseWhitelist
include SetTitle
end

当只有一个 DeviseWhitelist 时,我没有收到任何错误,但是当我尝试添加比它引发错误更多的问题时。

错误信息是:

无法自动加载常量 SetTitle,需要 /Users/User/Desktop/Rails/Account/app/controllers/concerns/set_title.rb 来定义它

标签: ruby-on-railsruby-on-rails-5.2

解决方案


问题是我在 IF/ELSE 语句下使用的大括号。我删除了它们,问题就解决了。


推荐阅读