参考 rails5 的 guides,下面的代码出现 Rails upload file error about“No such file or directory @ rb_sysopen,/Users/jayzen/workshop/upload_demo/public/uploads/demo.pdf”。
def upload
uploaded_io = params[:tag][:attachment]
File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'wb') do |file|
file.write(uploaded_io.read)
end
我对代码中 Rails.root.join 的参数个数做了修改,只是去掉“uploads”参数,代码如下,下面的代码按照预期运行,并且上传文件成功。
def upload
uploaded_io = params[:tag][:attachment]
File.open(Rails.root.join('public', uploaded_io.original_filename), 'wb') do |file|
file.write(uploaded_io.read)
end
问题:当 Rails.root.join 中的参数个数小于等于 2 时候,能够正常运行,但是参数个数大于 2 时,则会出现如上的问题,为何?
其他辅助代码
#支架
rails g scaffold tag name
#model中设置虚拟属性
class Tag < ActiveRecord
attr_accessor :attachment
end
#controller中设置attachment白名单
params.require(:tag).permit(:name, :attachment)
#controller中添加upload方法
class TagsController < ApplicationController
def create
@tag = Tag.new(tag_params)
if @tag.save
upload
......
end
private
def upload
....
end
end
环境:mac, ruby2.4.0, rails5.1.2