简体   繁体   English

回形针上传yml文件

[英]paperclip upload yml file

I have a problem with uploading yml file via paperclip. 我有通过paperclip上传yml文件的问题。 This is my how I try to do it: 这是我尝试这样做的方式:

In my model I have: 在我的模型中,我有:

has_attached_file :search_config
validates_attachment_content_type :search_config, content_type: ['text/yaml', 'application/x-yaml', 'text/x-yaml', 'application/yaml', 'text/plain']

In my view: 在我看来:

<%= form_for @search, :html => { :multipart => true } do |f| %> 

<fieldset>
    <legend>Skills</legend>
    <%= hidden_field_tag :type, 'recommendation' %>
    <%= f.file_field :search_config %>
    <%= f.submit 'Save', class: 'btn btn-primary' %>
</fieldset>

In my controller: 在我的控制器中:

def create
    @search = MailingSearch.new(params[:mailing_search])    
    if @search.save
        redirect_to action: :index, notice: 'Your search was successfully created. Search results will be send via email soon.'     
    else
        render action: :new
    end
end

I got an error, that 我收到了一个错误

search_config_content_type=>["is invalid"]

When I try to create a file from console (I'm using the same file): 当我尝试从控制台创建文件时(我使用的是同一个文件):

ms = MailingSearch.new
ms.search_config = File.open('tmp/test.yml')
ms.save

it works. 有用。 What can be a problem here? 这可能有什么问题?

Use the debugger to check params[:mailing_search] in your controller. 使用调试器检查控制器中的params[:mailing_search]

I suspect params[:mailing_search][:search_config].content_type will be application/octet-stream since a .yml file is treated as binary. 我怀疑params[:mailing_search][:search_config].content_type将是application/octet-stream因为.yml文件被视为二进制文件。

Since you don't allow application/octet-stream as a valid content type, that's why you get the error. 由于您不允许application/octet-stream作为有效的内容类型,因此您就会收到错误。

When you try through the console the content type is not being overridden by the browser, hence why it works. 当您尝试通过控制台时,浏览器不会覆盖内容类型,因此它的工作原理。

In your controller, before building @search , you can reset the content type in the params to the file's MIME type using: 在您的控制器中,在构建@search之前,您可以使用以下命令将params中的内容类型重置为文件的MIME类型:

params[:mailing_search][:search_config].content_type = MIME::Types.type_for(params[:mailing_search][:search_config].original_filename).first.content_type

This should preserve text/x-yaml as the content type for the file which you allow as valid. 这应该将text/x-yaml保留为您允许有效的文件的内容类型。

You do construct your MailingSearch in completely different ways in your example. 您可以在示例中以完全不同的方式构建MailingSearch。

In your controller you constantize something that is probably RecommendationMailingSearch and create an object of this class by passing it params[:mailing_search] which does not seem to be part of your form. 在您的控制器中,您可以对可能是RecommendationMailingSearch进行实例化,并通过传递params[:mailing_search]来创建此类的对象,而params[:mailing_search]似乎不是您表单的一部分。

In your manual example you instantiate an object of type MailingSearch and set a File object as the search_config . 在您的手动示例中,您实例化一个MailingSearch类型的对象,并将File对象设置为search_config

It is obvious that you are doing completely different things, how do you expect them to behave in the same way? 很明显,你正在做完全不同的事情,你怎么期望他们以同样的方式行事?

Maybe by improving your question such that the object classes match each other and the method calls are more similar you will find the answer on your own. 也许通过改进你的问题,使对象类相互匹配,方法调用更相似,你可以自己找到答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM