2013-11-24 20 views
0

他,我的代码存在问题,我现在正在寻找解决方案2个小时。我的红宝石是新上轨(和轨道..),我有这样的错误: NameError在PhotosController#新 未初始化的常量PhotosController ::照片RAILS:未初始化的常量PhotosController :: Photo [Ruby on rails]

这里我的代码,所以你可以帮我吧!

photos_controller.rb:

class PhotosController < ApplicationController 
    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 action: 'show', status: :created, location: @photo } 
     else 
     format.html { render action: '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 { head :no_content } 
     else 
     format.html { render action: '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 } 
     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) 
    end 
end 

photo.rb:

class Photo < ActiveRecord::Base 
has_attached_file :image 
end 

页I温度达到(//.../photos/new): new.html.erb:

<h1>New photo</h1> 

,我的路由文件:

PYL::Application.routes.draw do 
    get "photos/test" 
    get "photos/update" 
    get "photos/show" 
    get "photos/new" 
    resources :photos 
    get "photos/new" 
    # The priority is based upon order of creation: first created -> highest priority. 
    # See how all your routes lay out with "rake routes". 

    # You can have the root of your site routed with "root" 
    root 'photo#index' 

    # Example of regular route: 
    # get 'products/:id' => 'catalog#view' 

    # Example of named route that can be invoked with purchase_url(id: product.id) 
    # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase 

    # Example resource route (maps HTTP verbs to controller actions automatically): 
    # resources :products 

    # Example resource route with options: 
    # resources :products do 
    #  member do 
    #  get 'short' 
    #  post 'toggle' 
    #  end 
    # 
    #  collection do 
    #  get 'sold' 
    #  end 
    # end 

    # Example resource route with sub-resources: 
    # resources :products do 
    #  resources :comments, :sales 
    #  resource :seller 
    # end 

    # Example resource route with more complex sub-resources: 
    # resources :products do 
    #  resources :comments 
    #  resources :sales do 
    #  get 'recent', on: :collection 
    #  end 
    # end 

    # Example resource route with concerns: 
    # concern :toggleable do 
    #  post 'toggle' 
    # end 
    # resources :posts, concerns: :toggleable 
    # resources :photos, concerns: :toggleable 

    # Example resource route within a namespace: 
    # namespace :admin do 
    #  # Directs /admin/products/* to Admin::ProductsController 
    #  # (app/controllers/admin/products_controller.rb) 
    #  resources :products 
    # end 
end 

如果你有一个想法,请帮我:)

+0

编辑您的模型名称,它应该是单数照片... http://www.dummies.com/how-to/content/naming-conventions-for-ruby-on-rails .html – Bijendra

+0

我改变了它,但它没有解决我的问题! – Rabbit56

+0

模型文件名也应该是photo.rb和类照片应该用在里面 – Bijendra

回答

1

你命名你的模型类Photos而不是Photo。它应该是:

class Photo < ActiveRecord::Base 
    has_attached_file :image 
end 
+0

我改变了它,但它没有解决我的问题! – Rabbit56

+0

@ user3028968放置您的photo.rb文件在哪里? –

+0

哦!对不起!这不是在良好的文件夹。我错误地把它放在一个坏的!现在它在Model文件夹中,每一件事情都是找到的。谢谢 ! – Rabbit56

相关问题