简体   繁体   English

如何在Rails应用程序中将file_field连接到上载操作

[英]How to connect a file_field to the upload action in a rails app

I'm trying to add to a very simple data entry form, a button that allows the user to upload a file to the servers file system so that the url to that file can be placed in a field of the database. 我试图将一个按钮添加到一个非常简单的数据输入表单中,该按钮允许用户将文件上传到服务器文件系统,以便将该文件的url放置在数据库的字段中。

So far, I've worked out that in the _form...erb file, I can add the file_field helper: 到目前为止,我已经确定在_form ... erb文件中,可以添加file_field帮助器:

<%= form_for(@business) do |f| %>
<% if @business.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@business.errors.count, "error") %> prohibited this business from being saved:</h2>

<ul>
<% @business.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :deletedFlag %><br />
<%= f.check_box :deletedFlag %>
</div>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>

...

<div class="field">
<%= f.label :logoURL %><br />
<%= f.text_field :logoURL %>

    <%= f.file_field :imagefile %>
</div>

<div class="field">
<%= f.label :streetAddress %><br />
<%= f.text_area :streetAddress %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

This gives me a form with a "Choose file" button. 这给了我一个带有“选择文件”按钮的表格。 Clicking on that allows the selection of the file. 单击该按钮可以选择文件。 Great. 大。

Now, from the ruby guide at: http://guides.rubyonrails.org/form_helpers.html#uploading-files I'm told to add something like: 现在,从http://guides.rubyonrails.org/form_helpers.html#uploading-files上的ruby指南中,我被告知要添加类似以下内容:

def upload
  uploaded_io = params[:business][:imagefile]
  File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'w') do |file|
    file.write(uploaded_io.read)
  end
end

to my controller. 给我的控制器。

What I haven't worked out is how that file_field gets connected to that 'upload' action. 我还没有解决的是该file_field如何连接到该“上载”操作。 I can see in the logs that the button press accepts the filename, etc, but there's no indication that the file upload is started or attempted. 我在日志中看到按按钮会接受文件名等,但是没有迹象表明文件开始或尝试上传。

I've read a number of references, and they all seem to say that you need these two things, but (for me) they don't connect the dots. 我已经阅读了许多参考资料,它们似乎都说您需要这两件事,但是(对我而言)它们之间没有联系。 Yes I'm very new to Ruby/Rails, so I'm obviously missing something fundamental. 是的,我是Ruby / Rails的新手,因此显然缺少一些基本知识。

You probably need to setup a route so that Rails can pass the request from your form into your controller, then setting up your form to use that route: 您可能需要设置一条路由,以便Rails可以将请求从表单传递到控制器中,然后设置表单以使用该路由:

# config/routes.rb
post '/upload' => 'mycontroller#upload'

# app/views/some_model/new.html.erb
<%= form_for something, url: upload_path do |f| %>
...

This is probably the least recommended way of handling a file upload on Rails, especially for a new user, though. 不过,这可能是在Rails上处理文件上传的最不推荐的方法,特别是对于新用户。 You would be much better off setting up Carrierwave , Paperclip , or Dragonfly instead. 相反 ,最好设置CarrierwavePaperclipDragonfly

Did you forget to specify multipart in your form_for like this? 您是否忘了像这样在form_for中指定多部分?

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

Check the generated form html code, do you see this? 检查生成的表单html代码,您看到了吗?

<form ... enctype="multipart/form-data" ...>

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

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