在3.2到4的导轨上传文件时遇到问题 [英] Trouble with uploading file after upgrating rails from 3.2 to 4
问题描述
在我的画廊上传文件与Rails 3一起工作良好,但在提高后,我得到了这一行的exeption 错误的参数类型ActionDispatch :: Http :: UploadedFile(expected String)
@ reel.update_attributes(reels_params)
。
我不使用任何gem来处理文件上传,因为我需要特定的处理并将我的文件存储在数据库中。在升级到rails 4之后,我只添加了一个方法来处理控制器 reels_params
中的强参数。
控制器:
class Admin :: ReelsController< AdminController
before_action:set_reel,only:[:show,:edit,:update,:destroy]
def update
respond_to do | format |
if @ reel.update_attributes(reels_params)
format.html {redirect_to admin_reel_path(@reel),notice:t('helpers.messages.update_success')}
else
format .html {render action:edit}
end
end
end
...
private
def set_reel
@reel = Reel.find(params [:id])
end
def reels_params
params.require(:reel).permit!
end
end
查看:
= form_for [:admin,@reel],:html => {:class => 'form-horizontal form-admin',:multipart => true} do | f |
.form-group
= f.label'Gallery',:class => 'control-label col-sm-2'
.col-sm-10
- item.images.each do | img |
= f.fields_for:images,img do | img_f |
= img_f.text_field:title
= img_f.file_field:data,class:'img_upload'
如何纠正我的代码?
为了解决这个问题,单个图像元素
pre $ <%= form_tag({action::update},multipart:true,method:patch )do%>
<%= file_field_tag'图片'%>
< div class =actions>
<%= submit_tag%>
< / div>
<%end%>
与控制器:
def update
@ user.image = params [:image]<< ------在这一行失败
respond_to do | format |
if @ user.save ....
错误是:
错误的参数类型ActionDispatch :: Http :: UploadedFile(expected String)
在这里:
http://api.rubyonrails.org/classes/ActionDispatch/Http/UploadedFile.html
...我们找到.read选项,所以我修改了控制器:
def update
@ user.image = params [:image] .read<< ------在这里添加后缀
respond_to do | format |
if @ user.save ...
它可以工作,我可以读取图像从文件返回OK。因此,看起来我们需要单独隔离和保存blob,添加.read后缀,并且不能将其保存在具有其他参数的组中,直到此BUG /(功能)被修复。
Uploading files in my gallery worked fine with Rails 3, but after upgrate I get exeption wrong argument type ActionDispatch::Http::UploadedFile (expected String)
in this line @reel.update_attributes(reels_params)
.
I don't use any gem for handle file uploading, because I need specific processing and store my files in db. After upgraded to rails 4, I added only 1 method to working with strong params in controller reels_params
.
Controller:
class Admin::ReelsController < AdminController
before_action :set_reel, only: [:show, :edit, :update, :destroy]
def update
respond_to do |format|
if @reel.update_attributes(reels_params)
format.html { redirect_to admin_reel_path(@reel), notice: t('helpers.messages.update_success') }
else
format.html { render action: "edit" }
end
end
end
...
private
def set_reel
@reel = Reel.find(params[:id])
end
def reels_params
params.require(:reel).permit!
end
end
View:
= form_for [:admin, @reel], :html => { :class => 'form-horizontal form-admin', :multipart => true } do |f|
.form-group
= f.label 'Gallery', :class => 'control-label col-sm-2'
.col-sm-10
- item.images.each do |img|
= f.fields_for :images, img do |img_f|
= img_f.text_field :title
= img_f.file_field :data, class: 'img_upload'
How to correct my code?
To break down this problem, I took the view down to only the single image-element
<%= form_tag({action: :update}, multipart: true, method: "patch") do %>
<%= file_field_tag 'image' %>
<div class="actions">
<%= submit_tag %>
</div>
<% end %>
with controller:
def update
@user.image = params[:image] <<------Fails on this line
respond_to do |format|
if @user.save ....
The Error is: "wrong argument type ActionDispatch::Http::UploadedFile (expected String)"
Looking here: http://api.rubyonrails.org/classes/ActionDispatch/Http/UploadedFile.html ... we find the ".read" option, so I modified the controller to read:
def update
@user.image = params[:image].read <<------added the suffix here
respond_to do |format|
if @user.save ...
It works, and I can read the image back from the file OK. So, it appears we will need to isolate and save blobs separately, adding the ".read" suffix, and cannot save it in a group with other params until this BUG/(feature) is fixed.
这篇关于在3.2到4的导轨上传文件时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!