2017-06-02 5 views
0

iam mit Carierwave, um eine mehrteilige/Formulardaten zu buchen. dies mein Skript istCarierwave erhalten Null auf hochgeladenen Dateipfad

#imagepath model 
    class Imagepath < ActiveRecord::Base 
     belongs_to :imagepost 
     attr_accessor :path 
     mount_uploader :path, ImagepathUploader 
    end 

#imagepost model 
class Imagepost < ActiveRecord::Base 
    belongs_to :user 
    has_many :imagepaths 
    has_many :imagecomments 
    has_many :imagelikes 
    attr_accessor :imagepath_data 
    # attr_accessor :path 
end 

    #imagepost controller post method 
    # POST /imageposts 
    def create 
    @imagepost = Imagepost.new(imagepost_params) 

    if @imagepost.save 
     params[:imagepost][:imagepath_data].each do |file| 
     @imagepost.imagepaths.create!(:path => file) 
     end 

     render json: @imagepost, status: :created, location: @imagepost 
    else 
     render json: @imagepost.errors, status: :unprocessable_entity 
    end 
    end 


#imagepost_params for post_params 
def imagepost_params 
    params.require(:imagepost).permit(:title, :description, :user_id, :imagepath_data => []) 
end 

iam mit curl die Daten

curl 
-F "imagepost[imagepath_data][]=c4ewt.JPG" 
-F "imagepost[imagepath_data][]=border-image.png" 
-F "imagepost[title]=asasassasa" 
-F "imagepost[description]=uhuhuhuhuhuhuh" 
-F "imagepost[user_id]=5" localhost:3000/imageposts 

Posting ist Arbeit zu schreiben, aber nach Post zu tun, erhalte ich Pfad Reihe von meiner imagepath Tabelle ist null :( enter image description here

Antwort

1

Wahrscheinlich haben Sie eine @ verloren:

-F "imagepost[imagepath_data][][email protected]" 

Edit: auch wäre es besser mit Rails Konsole bin/rails c zu starten und inspizieren Sie Ihre DB: Imagepath.find(17). Es zeigt Ihnen, was tatsächlich gespeichert wurde.

Ich schlage vor, Active Record Nested Attributes und curl -v Option für ausführliche Ausgabe zu verwenden. ist hier beispielsweise aus realen Projekt vereinfacht:

report.rb

class Report < ApplicationRecord 
    # ... 
    accepts_nested_attributes_for :report_images, 
    reject_if: proc { |attributes| attributes[ 'image' ].blank? } 
end 

report_image.rb

class ReportImage < ApplicationRecord 
    # ... 
    mount_uploader :image, ReportImageUploader 
end 

reports_controller.rb

class ReportsController < YourBaseController 
    # ... 
    def create 
    # In real project service class is used 
    report = Report.create!(create_params) 
    # ... 
    end 

    private 

    def create_params 
    params 
     .require(:report) 
     .permit( 
     :my_report_attribute, 
     report_images_attributes: [ :kind, :image ]) 

    end 
end 

und dann curl:

curl -XPOST -v http://lvh.me:3000/yourendpoint/reports \ 
    -F "report[my_report_attribute]=Hehe" \ 
    -F "report[report_images_attributes][0][kind]=haha" \ 
    -F "report[report_images_attributes][0][image][email protected]/Users/myuser/my0.jpg" \ 
    -F "report[report_images_attributes][1][kind]=hoho" \ 
    -F "report[report_images_attributes][1][image][email protected]/Users/myuser/my1.png" 
+0

Dank jetzt seine gelöst. Ich fügte auch serialize hinzu: Avatare, JSON auf meinem imagepaths Modell und arbeitete jetzt. Ich verurteile SQLite – cahyowhy

Verwandte Themen