我正在尝试使用和学习载波。我的问题是我能够上传文件,但在尝试编辑时我收到错误uninitialized constant ImageFile
继承我的images_controller.rb
class ImagesController < ApplicationController
before_action :set_image, only: [:show, :edit, :update, :destroy]
# GET /images
# GET /images.json
def index
@images = Image.all
end
# GET /images/1
# GET /images/1.json
def show
end
# GET /images/new
def new
@image = Image.new
end
# GET /images/1/edit
def edit
end
# POST /images
# POST /images.json
def create
@image = Image.new(image_params)
respond_to do |format|
if @image.save
format.html { redirect_to @image, notice: 'Image was successfully created.' }
format.json { render :show, status: :created, location: @image }
else
format.html { render :new }
format.json { render json: @image.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /images/1
# PATCH/PUT /images/1.json
def update
respond_to do |format|
if @image.update(image_params)
format.html { redirect_to @image, notice: 'Image was successfully updated.' }
format.json { render :show, status: :ok, location: @image }
else
format.html { render :edit }
format.json { render json: image.errors, status: :unprocessable_entity }
end
end
end
# DELETE /images/1
# DELETE /images/1.json
def destroy
@image.destroy
respond_to do |format|
format.html { redirect_to images_url, notice: 'Image was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_image
@image = Image.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def image_params
params.require(:image).permit(:Title, :ImageFile, :Comments)
end
end
这是在new和edit
中使用的_form.html.erb<%= form_for @image, html: { multipart: true } do |f| %>
<% if @image.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@image.errors.count, "error") %> prohibited this image from being saved:</h2>
<ul>
<% @image.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :Title %><br>
<%= f.text_field :Title %>
</div>
<div class="field">
<%= f.label :ImageFile %><br>
<%= f.file_field :ImageFile %>
</div>
<div class="field">
<%= f.label :Comments %><br>
<%= f.text_field :Comments %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
这是image.rb
class Image < ActiveRecord::Base
# attr_accessible :Comments, :ImageFile, :Title
mount_uploader :ImageFile, ImageCreatorUploader
end
请帮助
答案 0 :(得分:0)
您的Image
课程出错了。您没有正确安装上传器。您将类名的符号传递给mount_uploader
方法。您需要传递字段名称的符号。替换:
class Image < ActiveRecord::Base
mount_uploader :ImageFile, ImageCreatorUploader
end
使用:
class Image < ActiveRecord::Base
mount_uploader :image_file, ImageCreatorUploader
end
方法名称不应大写。只有班级名称。
此外,由于您使用的是strong_parameters,因此您不需要attr_accessible
。你已经评论了这条线,但我想我会指出这一点。您还在此处将您的属性名称大写,这是不正确的。使用snake_case
。
答案 1 :(得分:0)
在ruby中,属性名称,变量名称,方法名称都应以小写字母开头。以大写字母开头的名称被视为常量和类名。
将对班级属性(:Title
,:ImageFile
,:Comments
)的所有引用更改为snake_case
(:title
,:image_file
,{{ 1}})