0
私はRoRのAPIでCarrierwaveを統合しようとしている、と私は、このエラーメッセージに遭遇:ルビー:ActiveRecordは:: RecordInvalid(検証は失敗しました:アバターは空白にすることはできません):
ActiveRecord::RecordInvalid (Validation failed: Avatar can't be blank):
app/controllers/api/v1/user_controller.rb:10:in `upload'
ユーザーをコントローラ:
module Api
module V1
class UserController < ApplicationController
before_action :set_user, only: [:show, :update, :destroy]
#before_filter :authenticate_user_from_token!
def upload
@user = User.find_by(email: params[:user][:email])
@user.avatar = params[:user][:file]
@user.save!
p @user.avatar.url # => '/url/to/file.png'
p @user.avatar.current_path # => 'path/to/file.png'
p @user.avatar_identifier # => 'file.png'
end
...
出力用:
params[:user][:email] => [email protected]
params[:user][:file] => #<File:0x007fbacc083318>
API呼び出しが残り-CLIENを使用して行われますt宝石:
...
response = RestClient.post 'http://' + @host.to_s + ':' + @port.to_s + '/api/user/upload', {'user' => { 'email' => email, 'file' => File.new("/Users/admin/Desktop/me.jpg", 'rb')}}.to_json, :content_type => :json
...
私は投稿が正しくないと感じています。
多くの感謝!
ファイルを自分で保存することはできません。すべて保存する必要があります。これは間違っている '@user.avatar = params [:user] [:file]' – 7urkm3n
はい、これは本当です。 Request = RestClient :: Request.new( :method =>:投稿、 :url => 'http)リクエストが正しくビルドされていないことを発見しました(私は使っていませんでした:multipart => true) ://」+ host.to_s + ':' + port.to_s + '/ API /ユーザー/アップロード'、 :ペイロード=> { :マルチパート=>真、 :メール=>電子メール、 :ファイル= >ファイル }、 :accept => accept.to_s) –