0

I carrierwave bin mit einem Bild von einem Python-API zu übernehmen. Ich habe den folgenden Code:carrierwave arbeitet mit Form, aber nicht API in Rails 5

def create 
    @person = Person.new(person_params) 

    respond_to do |format| 
     if @person.save 
     format.html { redirect_to @person, notice: 'Person was successfully created.' } 
     format.json { render :show, status: :created, location: @person } 
     else 
     format.html { render :new } 
     format.json { render json: @person.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

def person_params 
    params.permit(:client_file_name, :name, :picture) 
end 

Ich nehme params.require(:person) musste, weil die Python-Bibliothek ich requests genannt bin mit Ihnen nicht erlaubt, Dateien in dieser Form zu senden.

Nach den Server-Logs, das Bild zu speichern:

Started POST "/people" for 127.0.0.1 at 2017-11-19 11:49:19 -0500 
Processing by PeopleController#create as */* 
    Parameters: {"picture"=>#<ActionDispatch::Http::UploadedFile:0x007fe5e8e900b8 @tempfile=#<Tempfile:/var/folders/77/2f_jjrld0dxgq3t8xv6clvph0000gn/T/RackMultipart20171119-13141-1nunpha.png>, @original_filename="Phil.png", @content_type=nil, @headers="Content-Disposition: form-data; name=\"picture\"; filename=\"Phil.png\"\r\n">} 
    (0.0ms) begin transaction 
    SQL (0.2ms) INSERT INTO "people" ("picture", "created_at", "updated_at") VALUES (?, ?, ?) [["picture", "Phil.png"], ["created_at", "2017-11-19 16:49:19.776189"], ["updated_at", "2017-11-19 16:49:19.776189"]] 
    (0.6ms) commit transaction 
Redirected to http://localhost:3000/people/8 
Completed 302 Found in 15ms (ActiveRecord: 1.5ms) 

ich nur diese lokal tue. Wenn ich in das Verzeichnis schaue, in dem das Bild gespeichert werden soll, gibt es dort ein Bild namens Phil.png was passieren sollte, außer das Bild hat null Bytes.

Wenn ich das gleiche Bild mit einem Formular laden, funktioniert alles wie erwartet. Wie behebe ich das?

+0

Ich bin mir nicht sicher, ob es relevant ist. Der Enktyp ist 'multipart/form-data'? Ich hatte einmal ein ähnliches Problem, aber erinnere mich nicht, ob es ein Bild mit 0 Bytes gab. –

Antwort

0

Also habe ich es herausgefunden. Rails erfordert, dass Sie den Inhaltstyp auf image/png festlegen, damit das Problem durch Hinzufügen der Anfrage gelöst wird.

Verwandte Themen