2017-05-27 1 views
0

Erstellen einer mobilen App mit Rails Backend, ich möchte das Facebook Profilbild von Facebook zu meinem eigenen System mit Schienen importieren. Das Profilbild befindet sich an einer bestimmten URL, und die Bilder in meinem Backend werden mit Refile gespeichert.Erstellen Refile Anhang von HTTpary auf einem Ruby-Modell mit Mongo db

Das ProfilePicture Modell:

class ProfilePicture 
    include Mongoid::Document 
    extend Refile::Mongoid::Attachment 
    attachment :file, type: :image 
    belongs_to :user 

    field :created_at, type: String 
    field :updated_at, type: String 
end 

Benutzermodell:

class User 
    include Mongoid::Document 

    field :created_at, type: String 
    field :updated_at, type: String 

    has_one :profile_picture 
end 

Der Code, um das Bild zu setzen, mit HTTParty:

@user = User.find_by(id: 1) 
@picture_data = HTTParty.get("https://graph.facebook.com/me/picture", query: { 
    access_token: access_token, 
    type: "large" 
}) 
pic = ProfilePicture.new(file: @picture_data.body) 
@user.update(profile_picture: pic) 

Das Profilbild ist tierten, wie Ich kann die Daten im HTTP-Ergebnis sehen. Das DB-Ergebnis der obigen "Update" -Aktion wäre ein neuer ProfilePicture-Datensatz im mongodb mit einem Verweis auf die Benutzerdaten, also auch OK. Das einzige Problem ist, dass der Anhang nicht gespeichert wird.

Wie speichere ich den Anhang?

Antwort

0

Die Antwort war, dass die Bilddatei, die von Facebook geholt wurde, kein gültiger "Bild" -Typ für den Anhang war. Die Änderung bestand darin, den Typ wie folgt aus der Anhangseigenschaft zu entfernen:

class ProfilePicture 
    include Mongoid::Document 
    extend Refile::Mongoid::Attachment 
    attachment :file 
    belongs_to :user 

    field :created_at, type: String 
    field :updated_at, type: String 
end