2017-04-13 51 views
0

我试图将自定义照片模型添加到大型商店管理面板,当我尝试转到页面时出现错误:未初始化的常量管理员。我有一种感觉,它与我如何命名我的文件或我将它们放在哪里有关。这是我到目前为止有:将自定义模型添加到spree并获取错误:管理

路线

namespace :admin do 
    resources :photos 
end 

我的视图文件都位于:应用程序/视图/大礼包/管理/照片

应用程序/控制器/管理/ 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' 
+0

什么是错误的完整描述? –

+0

@alejandroMontilla我刚更新了完整的错误信息 – Kris

+0

看起来像一个照片控制器的问题,检查名称和路径(应该像'app/controllers/admin/photos_controller.rb') –

回答

0

您需要使用:

class PhotosController < BaseController 

既然你都在里面admin命名空间已经

1

那么,你需要使用适当的模块scoping.This代码段会帮助你。

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,这将非常有帮助。

相关问题