大家好我在我的rails应用程序上使用carrierwave从计算机上传图像以创建新项目..如果我创建项目而不上传图像,该项目将保存到数据库中,否则它赢了& #39;吨。所以我的载波设置一定有问题。谁能帮我一把,找出错误?
表格HTML
<form action="/items" method="post" enctype='multipart/form-data'>
<%= hidden_field_tag :authenticity_token, form_authenticity_token %>
<label>Name</label>
<input type="text" name="name">
<label>Size</label>
<input type="text" name="size">
<label>Image</label>
<input type="file" name="image">
<label>Description</label>
<textarea name="description"></textarea>
<label>Price</label>
<input type="number" name="price" placeholder="price $$">
<button> Save </button>
</form>
IMAGE UPLOADER
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :thumb do
process :resize_to_fit => [150, 150]
end
def extension_white_list
%w(jpg jpeg gif png)
end
end
安装了创业板
gem 'carrierwave'
gem "mini_magick"
项目模型
class Item < ActiveRecord::Base
mount_uploader :image, ImageUploader
belongs_to :category
end
ITEM CONTROLLER
class ItemsController < ApplicationController
def index
@items = Item.all
end
def create
item = Item.new
item.name = params[:name]
item.image = params[:image]
item.size = params[:size]
item.description = params[:description]
item.price = params[:price]
if item.save
render :show
else
puts "error"
end
end
def show
@item = Item.find(params[:id])
end
def update
@item = Item.find(params[:id])
@item.name = params[:name]
@item.image = params[:image]
@item.size = params[:size]
@item.description = params[:description]
@item.price = params[:price]
end
def destroy
@item = Item.find(params[:id])
@item.destroy
end
end
答案 0 :(得分:0)
更改此行:
<form action="/items" method="post" enctype='multipart/form-data'>
到这一个:
<%= form_for Item.new, method: 'post', multipart=> true do %>
不要忘记关闭<%= form_for %>
结束标记。希望这会有所帮助。
如果您可以发布development.log
文件输出,将会很有帮助。
N.B。请重新格式化您的代码。这是有史以来最烦人的代码。