将自定义模型添加到spree并获取错误:未初始化的常量Admin

时间:2017-04-13 20:05:36

标签: ruby-on-rails spree

我正在尝试将自定义照片模型添加到狂欢商店管理面板中,当我尝试转到该页面时,我收到错误:未初始化的常量管理员。我有一种感觉,它与我如何命名我的文件或我把它们放在哪里有关。这就是我到目前为止所做的:

路线

namespace :admin do
   resources :photos
end

我的观看文件全部位于:app / views / spree / admin / photos

应用程序/控制器/管理/ photos_controller.rb

module Admin
    class PhotosController < Admin::BaseController
      before_action :set_photo, only: [:show, :edit, :update, :destroy]

  # GET /photos
  # GET /photos.json
  def index
    @photos = Photo.all
  end

  # GET /photos/1
  # GET /photos/1.json
  def show
  end

  # GET /photos/new
  def new
    @photo = Photo.new
  end

  # GET /photos/1/edit
  def edit
  end

  # POST /photos
  # POST /photos.json
  def create
    @photo = Photo.new(photo_params)

    respond_to do |format|
      if @photo.save
        format.html { redirect_to @photo, notice: 'Photo was successfully created.' }
        format.json { render :show, status: :created, location: @photo }
      else
        format.html { render :new }
        format.json { render json: @photo.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /photos/1
  # PATCH/PUT /photos/1.json
  def update
    respond_to do |format|
      if @photo.update(photo_params)
        format.html { redirect_to @photo, notice: 'Photo was successfully updated.' }
        format.json { render :show, status: :ok, location: @photo }
      else
        format.html { render :edit }
        format.json { render json: @photo.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /photos/1
  # DELETE /photos/1.json
  def destroy
    @photo.destroy
    respond_to do |format|
      format.html { redirect_to photos_url, notice: 'Photo was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_photo
      @photo = Photo.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def photo_params
      params.require(:photo).permit(:image, :book_id)
    end





    end
  end

以下是完整错误:

Started GET "/admin/photos" for 207.93.212.56 at 2017-04-13 19:18:41 +0000
Cannot render console from 207.93.212.56! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255

ActionController::RoutingError (uninitialized constant Admin):
  activesupport (4.2.6) lib/active_support/inflector/methods.rb:261:in `const_get'
  activesupport (4.2.6) lib/active_support/inflector/methods.rb:261:in `block in constantize'
  activesupport (4.2.6) lib/active_support/inflector/methods.rb:259:in `each'
  activesupport (4.2.6) lib/active_support/inflector/methods.rb:259:in `inject'
  activesupport (4.2.6) lib/active_support/inflector/methods.rb:259:in `constantize'
  actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:70:in `controller_reference'
  actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:60:in `controller'
  actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:39:in `serve'
  actionpack (4.2.6) lib/action_dispatch/journey/router.rb:43:in `block in serve'
  actionpack (4.2.6) lib/action_dispatch/journey/router.rb:30:in `each'
  actionpack (4.2.6) lib/action_dispatch/journey/router.rb:30:in `serve'
  actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:817:in `call'
  warden (1.2.7) lib/warden/manager.rb:36:in `block in call'
  warden (1.2.7) lib/warden/manager.rb:35:in `catch'
  warden (1.2.7) lib/warden/manager.rb:35:in `call'
  rack (1.6.5) lib/rack/etag.rb:24:in `call'
  rack (1.6.5) lib/rack/conditionalget.rb:25:in `call'
  rack (1.6.5) lib/rack/head.rb:13:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/flash.rb:260:in `call'
  rack (1.6.5) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.6.5) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/cookies.rb:560:in `call'
  activerecord (4.2.6) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.2.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
  activerecord (4.2.6) lib/active_record/migration.rb:377:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.2.6) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
  activesupport (4.2.6) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
  activesupport (4.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.6) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  web-console (2.3.0) lib/web_console/middleware.rb:20:in `block in call'
  web-console (2.3.0) lib/web_console/middleware.rb:18:in `catch'
  web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.6) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.6) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.6) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.6) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.5) lib/rack/methodoverride.rb:22:in `call'
  rack (1.6.5) lib/rack/runtime.rb:18:in `call'
  activesupport (4.2.6) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  rack (1.6.5) lib/rack/lock.rb:17:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/static.rb:120:in `call'
  rack (1.6.5) lib/rack/sendfile.rb:113:in `call'
  railties (4.2.6) lib/rails/engine.rb:518:in `call'
  railties (4.2.6) lib/rails/application.rb:165:in `call'
  rack (1.6.5) lib/rack/lock.rb:17:in `call'
  rack (1.6.5) lib/rack/content_length.rb:15:in `call'
  rack (1.6.5) lib/rack/handler/webrick.rb:88:in `service'
  /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/webrick/httpserver.rb:140:in `service'
  /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/webrick/httpserver.rb:96:in `run'
  /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/webrick/server.rb:296:in `block in start_thread'

2 个答案:

答案 0 :(得分:1)

好吧,你需要使用正确的模块范围。这段代码片段可以帮助你。

module Spree
  module Admin
    class PhotosController < BaseController
      before_action :set_photo, only: [:show, :edit, :update, :destroy]

      # GET /photos
      # GET /photos.json
      def index
        @photos = Photo.all
      end

      # GET /photos/1
      # GET /photos/1.json
      def show
      end

      # GET /photos/new
      def new
        @photo = Photo.new
      end

      # GET /photos/1/edit
      def edit
      end

      # POST /photos
      # POST /photos.json
      def create
        @photo = Photo.new(photo_params)

        respond_to do |format|
          if @photo.save
            format.html { redirect_to @photo, notice: 'Photo was successfully created.' }
            format.json { render :show, status: :created, location: @photo }
          else
            format.html { render :new }
            format.json { render json: @photo.errors, status: :unprocessable_entity }
          end
        end
      end

      # PATCH/PUT /photos/1
      # PATCH/PUT /photos/1.json
      def update
        respond_to do |format|
          if @photo.update(photo_params)
            format.html { redirect_to @photo, notice: 'Photo was successfully updated.' }
            format.json { render :show, status: :ok, location: @photo }
          else
            format.html { render :edit }
            format.json { render json: @photo.errors, status: :unprocessable_entity }
          end
        end
      end

      # DELETE /photos/1
      # DELETE /photos/1.json
      def destroy
        @photo.destroy
        respond_to do |format|
          format.html { redirect_to photos_url, notice: 'Photo was successfully destroyed.' }
          format.json { head :no_content }
        end
      end

      private
        # Use callbacks to share common setup or constraints between actions.
        def set_photo
          @photo = Photo.find(params[:id])
        end

        # Never trust parameters from the scary internet, only allow the white list through.
        def photo_params
          params.require(:photo).permit(:image, :book_id)
        end

        end
    end
  end
end

此外,我建议您从ResourceController而不是BaseController继承您的课程。通过这样做,您可以节省一些LOC,这将非常有用。

答案 1 :(得分:0)

您需要使用:

class PhotosController < BaseController

由于您已经在admin命名空间内