Homeland 看着源码操作的 controller 下的 application_controller.rb 出错

babyhai · 发布于 2018年01月19日 · 最后由 babyhai 回复于 2018年01月24日 · 461 次阅读

这是报错信息

RuntimeError in TopicsController#index
Circular dependency detected while autoloading constant Homeland::ApplicationController

module Homeland
  class ApplicationController < ::ApplicationController
    helper Homeland::ActionView::WillPaginate
    helper Homeland::ApplicationHelper

    helper_method :current_user, :owner?, :admin?

源码这里好像是没有写 ,不知道是不是漏掉了

共收到 13 条回复

:: 这个叫顶级作用域

刚刚才查到的 是顶层的命名空间;我想问问 我的这个报错是什么原因呢 不是很理解

huacnlee 回复

好的 谢谢

我还是不太明白啊 你发的链接我看过了 那是讲的rails怎么加载的 我这个是根本不知道这是什么原因导致的这个问题发生的 是没加载到 还是有什么依赖?

贴一下 TopicsController 的代码呢,这个问题通常是由省略了 namespace 导致的

我猜文件夹不对

TopicsController

module Homeland
  class TopicsController < Homeland::ApplicationController
    before_action :authenticate_user!, only: [:new, :edit, :create, :update, :destroy, :reply]
    before_action only: [:edit, :update, :destroy] do
      authorize_resource! topic
    end

    def index
      @topics = Topic.latest.includes(:user).page(params[:page])

      set_seo_meta(t("homeland.nav.latest"))
    end

    def node
      @node = Node.find(params[:id])
      @topics = @node.topics.latest.includes(:user).page(params[:page])

      render action: "index"
    end

    %w(recent features).each do |action|
      define_method(action) do
        @topics = Topic.send(action).includes(:user, :node).page(params[:page])

        set_seo_meta(t("homeland.nav.#{action}"))

        render action: "index"
      end
    end

    def show
      @topic = Topic.find(params[:id])
      @replies = replies

      set_seo_meta(@topic.title)
    end

    def new
      @topic = Topic.new
      @node = Node.find_by(id: params[:node_id])

      set_seo_meta(t("homeland.nav.new"))
    end

    def reply
      @topic = Topic.find(params[:id])
      @reply = @topic.replies.build(reply_params)
      @reply.user_id = current_user.id
      if @reply.save
        flash[:notice] = t('homeland.reply_created')
      else
        flash[:alert] = @reply.errors.full_messages.join("<br />")
      end
      redirect_to topic_path(params[:id],:anchor => "reply")
    end

    # GET /topics/1/edit
    def edit
      @node = @topic.node
    end

    # POST /topics
    # POST /topics.xml
    def create
      @topic = Topic.new(topic_params)
      @topic.user_id = current_user.id

      if @topic.save
        redirect_to(topic_path(@topic.id), notice: t('homeland.topic_created'))
      else
        render action: "new"
      end
    end

    # PUT /topics/1
    # PUT /topics/1.xml
    def update
      @topic = Topic.find(params[:id])

      if @topic.update_attributes(topic_params)
        redirect_to(topic_path(@topic.id), notice: t('homeland.topic_updated'))
      else
        render action: "edit"
      end
    end

    # DELETE /topics/1
    # DELETE /topics/1.xml
    def destroy
      topic.destroy
      redirect_to(topics_path, notice: t('homeland.topic_deleted'))
    end

    private

    def topic
      @topic ||= Topic.find(params[:id])
    end

    def replies
      @topic.replies.includes(:user).page(params[:page])
    end

    def topic_params
      params.require(:topic).permit(:node_id, :title, :body)
    end

    def reply_params
      params.require(:reply).permit(:body, :reply_to_id)
    end
  end
end

application_controller

module Homeland
  class ApplicationController < ::ApplicationController
    helper Homeland::ActionView::WillPaginate
    helper Homeland::ApplicationHelper

    helper_method :current_user, :owner?, :admin?

    alias_method :origin_current_user, Homeland.config.current_user_method.to_sym
    alias_method :origin_authenticate_user!, Homeland.config.authenticate_user_method.to_sym

    def current_user
      origin_current_user
    end

    def authenticate_user!
      origin_authenticate_user!
    end

    def authorize_resource!(obj)
      if !owner?(obj)
        redirect_to homeland.root_path, alert: t('homeland.access_denied')
      end
    end

    def authorize_admin!
      if !admin?
        redirect_to homeland.root_path, alert: t('homeland.access_denied')
      end
    end

    def set_seo_meta(title = '', meta_keywords = '', meta_description = '')
      @page_title = "#{title}" if title.length > 0
      @meta_keywords = meta_keywords
      @meta_description = meta_description
    end

    def owner?(obj)
      return false if obj.blank?
      return false if current_user.blank?
      return true if current_user.send(Homeland.config.user_admin_method) == true

      obj.user_id == current_user.id
    end

    def admin?
      return false if current_user.blank?
      current_user.send(Homeland.config.user_admin_method) == true
    end
  end
end

这是目录

class TopicsController < ApplicationController 改成这样试试

geekerzp 回复

好的

不行 还是报错

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册