【发布时间】:2018-02-23 22:44:21
【问题描述】:
第一次尝试通过 CarrierWave 上传图片时,我试图回想一下。我可以看到正在发送的数据,但随后在控制台中我还看到了操作中的 BEGIN 和 ROLLBACK。 所以,这就是我到目前为止所拥有的
控制器
class PhotosController < ApplicationController
before_action :set_photo, only: [:show, :edit, :update]
def index
@photos = Photo.all
end
def new
@photos = Photo.new
end
def create
@photo = Photo.new(photo_params)
if @photo.save
redirect_to photos_path, notice: "The photo #{@photo.name} has been uploaded."
else
render "new"
end
end
private
def photo_params
params.require(:photo).permit(:name, :image, :id)
end
def set_photo
@photo = Photo.find(params[:id])
end
end
表格
<div class="well">
<%= form_for Photo.new, html: { multipart: true } do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :image %>
<%= f.file_field :image %>
<%= f.submit "Save", class: "btn btn-primary" %>
<% end %>
</div>
型号
class Photo < ApplicationRecord
mount_uploader :image, ImageUploader
end
UPLOADER 几乎是生成的,存储是文件,它使用 MiniMagick 并启用拇指版本
现在,当我单击提交按钮时,我会在控制台中看到:
Started POST "/photos" for 127.0.0.1 at 2017-09-14 22:11:25 +0200
Processing by PhotosController#create as HTML
Parameters: {"utf8"=>"V", "authenticity_token"=>"Aj4CyJLaxh439igdVYJ60Pz9TqLzuvPVfkh903YAcaVuIqU8bKmX0zrgbL/GvnabcQ7i/hsqWxwvG1NRQO16JA==", "photo
"=>{"name"=>"fdsfds", "image"=>#<ActionDispatch::Http::UploadedFile:0xa96f1d0 @tempfile=#<Tempfile:C:/Users/Andrea/AppData/Local/Temp/RackMultipart2
0170914-4696-1pwrynm.jpg>, @original_filename="Clipboard02.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"photo[
image]\"; filename=\"Clipboard02.jpg\"\r\nContent-Type: image/jpeg\r\n">},
"commit"=>"Save"}
(1.0ms) BEGIN
(0.0ms) ROLLBACK
在我正在运行上传的页面上,它返回到新表单,并且文本显示在底部
<ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"....(manuallyremoved)==", "photo"=><ActionController::Parameters {"name"=>"fdsfds", "image"=>#<ActionDispatch::Http::UploadedFile:0xa96f1d0 @tempfile=#<Tempfile:C:/Users/Andrea/AppData/Local/Temp/RackMultipart20170914-4696-1pwrynm.jpg>, @original_filename="Clipboard02.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"photo[image]\"; filename=\"Clipboard02.jpg\"\r\nContent-Type: image/jpeg\r\n">} permitted: false>, "commit"=>"Save", "controller"=>"photos", "action"=>"create"} permitted: false>
那么在这种情况下究竟是什么导致了 allowed:false 呢? 我还注意到在我的 public/uploads/tmp 中创建了一个文件夹,并且图像实际上保存在该文件夹中
感谢您的任何建议
【问题讨论】:
标签: ruby-on-rails image file-upload carrierwave