我用 grape 写了一个更新 user 信息的 api,代码如下:
post "update" do
authenticate!
@user = current_user
user_params = ActionController::Parameters.new(params[:user]).permit(:name, :height, :weight, :avatar, :gender, :birthday, :province, :city, :district, :signature)
puts user_params
puts "------------\n"
puts params
puts "------------\n"
puts params[:user]
puts "------------\n"
puts params[:user][:avatar]
puts "------------\n"
if @user.update(user_params)
{ :success => 1 }
else
@user.errors.each do |e|
puts e
end
{ :success => 0 }
end
end
但是当我更新用户头像的时候,params[:user][:avatar] 失败了!生成的 user_params 为空!但是更新其他参数,就没有这个问题!例如更新性别 这是输出的结果:
{}
------------
#<Hashie::Mash route_info=#<Grape::Route:0x007fdafe732720 @options={:prefix=>"api", :version=>nil, :namespace=>"/users", :method=>"POST", :path=>"/api/users/update(.:format)", :params=>{}, :compiled=>/\A\/api\/users\/update(?:\.(?<format>[^\/.?]+))?\Z/}> user=#<Hashie::Mash avatar=#<Hashie::Mash filename="avatar.png" head="Content-Disposition: form-data; name=user[avatar]; filename=avatar.png\r\nContent-Type: image/png\r\n" name="user[avatar]" tempfile=#<Tempfile:/var/folders/7b/8z5t1ckx7g5brm_51yt99_g80000gn/T/RackMultipart20140726-34366-cwfqsu> type="image/png">>>
------------
#<Hashie::Mash avatar=#<Hashie::Mash filename="avatar.png" head="Content-Disposition: form-data; name=user[avatar]; filename=avatar.png\r\nContent-Type: image/png\r\n" name="user[avatar]" tempfile=#<Tempfile:/var/folders/7b/8z5t1ckx7g5brm_51yt99_g80000gn/T/RackMultipart20140726-34366-cwfqsu> type="image/png">>
------------
#<Hashie::Mash filename="avatar.png" head="Content-Disposition: form-data; name=user[avatar]; filename=avatar.png\r\nContent-Type: image/png\r\n" name="user[avatar]" tempfile=#<Tempfile:/var/folders/7b/8z5t1ckx7g5brm_51yt99_g80000gn/T/RackMultipart20140726-34366-cwfqsu> type="image/png">