Trouble with uploading file after upgrating rails from 3.2 to 4

亡梦爱人 提交于 2019-12-08 18:17:30

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!