【发布时间】:2013-12-16 04:58:50
【问题描述】:
在我的库中上传文件在 Rails 3 上运行良好,但升级后我在这一行 @reel.update_attributes(reels_params) 中得到异常 wrong argument type ActionDispatch::Http::UploadedFile (expected String)。
我不使用任何 gem 来处理文件上传,因为我需要特定的处理并将我的文件存储在 db 中。升级到 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'
如何更正我的代码?
【问题讨论】:
标签: ruby file-upload ruby-on-rails-4 strong-parameters