使用回形针和 simple_form 将文件上传到 rails 4.0 [英] uploading files to rails 4.0 with paperclip and simple_form

查看:32
本文介绍了使用回形针和 simple_form 将文件上传到 rails 4.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如@Justin 所说,我可能在试图找到解决方案时搞砸了,甚至没有意识到,哎呀……将参数更改为 params.require(:pin).permit(:image, :description) 确实解决了它.
虽然它现在不起作用,因为我得到一个具有与其内容不匹配的扩展名.

我正在关注 Rails 一个月,并试图将图像上传到 pin 脚手架中.我正在使用回形针作为文件和 simple_form.这是我认为相关的代码(请随时提出任何要求):

_pin_form.erb.html

<%= simple_form_for(@pin, :html => {:multipart => true}) do |f|%><%= f.error_notification %><div class="form-inputs"><%= f.file_field :image, :label =>上传图片"%><%= f.input :description, as: :text %>

<div class="form-actions"><%= f.button :submit %>

pin.rb

class Pin <ActiveRecord::Basehas_attached_file :图像归属地:用户验证 :description, :presence =>真的验证 :user_id, :presence =>真的validates_attachment :image, :presence =>真的# 验证内容类型validates_attachment_content_type :image, :content_type =>//\图片/# 验证文件名validates_attachment_file_name :image, :matches =>[/png\Z/,/jpe?g\Z/]# 明确不验证do_not_validate_attachment_file_type :image结尾

pin_params 来自 pin_controller.rb

def pin_paramsparams.permit(:description,:image)结尾

(其中大部分是当我尝试通过回形针自述文件进行故障排除时).每当我上传文件时,我都会收到一个错误提示

ActionController::ParameterMissing in PinsController#create未找到参数:图像

如果我尝试在某些东西上使用 params.require,我会收到一条错误消息,说 param not found.我不知道是什么原因造成的.如果我删除 pin_params 方法,表单字段只会返回不能为空",无论它们中有什么
这看起来很简单,但我找不到解决方案

解决方案

Paperclip 具有欺骗验证功能,以确保文件的内容与扩展名实际匹配.这种验证部分是通过使用操作系统的 file 命令来确定文件内容来实现的.不幸的是,Windows 没有 file 命令,因此验证将始终失败.

要解决此问题,您应该禁用欺骗.使用以下代码创建初始化程序:

模块回形针类 MediaTypeSpoofDetectordef欺骗?错误的结尾结尾结尾

离开 validates_attachment_content_type 验证器是有效的,因为它仍然是限制允许用户上传的文件类型的最佳方式.您只需要禁用此验证的欺骗位即可.

Edit: as @Justin said, I probably messed that up when trying to find a solution without even realizing, oopsy... Changing the params to params.require(:pin).permit(:image, :description) did solve it.
Though it doesn't work now because I get a "has an extension that does not match its contents.

I'm following Rails One Month and trying to get upload image into a pins scaffolding working. I'm using paperclip for files, and simple_form. This is the code I think is relevant (feel free to ask for anything):

_pin_form.erb.html

<%= simple_form_for(@pin, :html => {:multipart => true}) do |f| %>
 <%= f.error_notification %>

 <div class="form-inputs">
   <%= f.file_field :image, :label => "Upload an image" %>
   <%= f.input :description, as: :text %>
 </div>

 <div class="form-actions">
   <%= f.button :submit %>
 </div>

pin.rb

class Pin < ActiveRecord::Base

 has_attached_file :image
 belongs_to :user

 validates :description, :presence => true
 validates :user_id, :presence => true
 validates_attachment :image, :presence => true

# Validate content type
 validates_attachment_content_type :image, :content_type => /\Aimage/
# Validate filename
 validates_attachment_file_name :image, :matches => [/png\Z/, /jpe?g\Z/]
# Explicitly do not validate
 do_not_validate_attachment_file_type :image
end

pin_params from pins_controller.rb

def pin_params
  params.permit(:description, :image)
end

(most of these is when I tried troubleshooting through paperclip README). Whenever I upload a file I get an error saying

ActionController::ParameterMissing in PinsController#create
param not found: image

and if I try to use params.require on something I get an error saying param not found. I have no idea what is causing it. If I remove the pin_params method the form fields just return "can't be blank" no matter what's in them"
This seems simple but I just can't find a solution to this

解决方案

Paperclip has spoofing validation to ensure the contents of the file actually match the extension. This validation is achieved partially by using the file command of the operating system to determine the file content. Unfortunately Windows doesn't have a file command so the validation will always fail.

To workaround this you should disable spoofing. Create an initializer with the following code:

module Paperclip
  class MediaTypeSpoofDetector
    def spoofed?
      false
    end
  end
end

It's valid to leave the validates_attachment_content_type validator though because is it still the best way to restrict the types of files the user is allowed to upload. You just need to disable the spoofing bit of this validation.

这篇关于使用回形针和 simple_form 将文件上传到 rails 4.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆