Ruby on rails RAILS:未初始化的常量PhotosController::Photo[Ruby on RAILS]
他,我的代码有问题,我正在寻找一个2小时的解决方案。我是RubyonRails(和rails…)的新手,我有一个错误: PhotosController中的名称错误#新建 未初始化的常量PhotosController::Photo 这是我的代码,你可以帮我 照片\u controller.rb:Ruby on rails RAILS:未初始化的常量PhotosController::Photo[Ruby on RAILS],ruby-on-rails,ruby,model,controller,Ruby On Rails,Ruby,Model,Controller,他,我的代码有问题,我正在寻找一个2小时的解决方案。我是RubyonRails(和rails…)的新手,我有一个错误: PhotosController中的名称错误#新建 未初始化的常量PhotosController::Photo 这是我的代码,你可以帮我 照片\u controller.rb: class PhotosController < ApplicationController before_action :set_photo, only: [:show, :edit,
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
class Photo < ActiveRecord::Base
has_attached_file :image
end
<h1>New photo</h1>
如果您有什么想法,请帮助我:)您将模型类命名为
Photos
,而不是Photo
。应该是:
class Photo < ActiveRecord::Base
has_attached_file :image
end
class-Photo
您将模型类命名为Photos
,而不是Photo
。应该是:
class Photo < ActiveRecord::Base
has_attached_file :image
end
class-Photo
编辑您的型号名称,它应该是单张照片…我更改了它,但没有解决我的问题!模型文件名也应该是photo.rb,类photo应该在itIt的find now thank中使用。我将photo.rb文件放在错误的文件夹中,错误地编辑了您的模型名称,它应该是单张照片…我更改了它,但它没有解决我的问题!模型文件名也应该是photo.rb,类photo应该在itIt的find now thank中使用。我把photo.rb文件放错文件夹了mistake@user3028968你的photo.rb文件放在哪里了?哦!很抱歉它不在好文件夹中。我把它错放在一个坏的里面了!现在它在Model文件夹中,所有东西都可以找到了。谢谢大家!@user3028968你的photo.rb文件放在哪里?哦!很抱歉它不在好文件夹中。我把它错放在一个坏的里面了!现在它在Model文件夹中,所有东西都可以找到了。非常感谢。