新手问题 这个怎么保存文件到 carrierwave

vorfeed · 2016年01月12日 · 最后由 lionzixuanyuan 回复于 2016年01月12日 · 2274 次阅读

Parameters: {"file"=>[#, @original_filename="0D936BE7B1D662DD950215057C6C9C00.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"file[]\"; filename=\"0D936BE7B1D662DD950215057C6C9C00.jpg\"\r\nContent-Type: image/jpeg\r\n">]}

是用 ajax 提交上去的

params[:file]

TypeError (no implicit conversion of nil into String): 报这个错误

你不是招聘贴里常出现的“远程要吗”机器人吗?居然发帖了。。。。

根据你的数据 params[:file] 是一个数组。剩下的你自己理解吧。

你那个 file_field是不是加了multiple: true…… 官方文档说的是:

Make sure your file input fields are set up as multiple file fields. For example in Rails you'll want to do something like this:

<%= form.file_field :avatars, multiple: true %>

Also, make sure your upload controller permits the multiple file upload attribute, pointing to an empty array in a hash. For example:

params.require(:user).permit(:email, :first_name, :last_name, {avatars: []})

Now you can select multiple files in the upload dialog (e.g. SHIFT+SELECT), and they will automatically be stored when the record is saved.

u = User.new(params[:user])
u.save!
u.avatars[0].url # => '/url/to/file.png'
u.avatars[0].current_path # => 'path/to/file.png'
u.avatars[0].identifier # => 'file.png'
需要 登录 后方可回复, 如果你还没有账号请 注册新账号