2017-05-12 5 views
0

Ich habe zwei Modelle:Nested Attribute mit Bild-Upload

class Campaign < ApplicationRecord 
    has_many :questions 
    validates_presence_of :name 

    accepts_nested_attributes_for :questions 
end 


class Question < ApplicationRecord 
    belongs_to :campaign 
    has_many :answered_questions 
    has_many :answers, through: :answered_questions 


    has_attached_file :image, :storage => :cloudinary, :path => ':id/:style/:filename', styles: { medium: "300x300>", thumb: "100x100>" } 
    validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/ 

    validates_presence_of :title, :image, :campaign_id 

    validates_associated :campaign 

    def image_url 
     image.url(:thumb) 
    end 

end 

ich eine Frage in der gleichen _form erstellen möchten, wo i Controller CAMPAIGN

CAMPAIGN

def new 
     @campaign = Campaign.new 
     @questions_builded = @campaign.questions.build 
    end 

    def create 
     @campaign = Campaign.new(campaign_params) 

     respond_to do |format| 
      if @campaign.save 
      format.html { redirect_to @campaign, notice: 'Campanha foi criada com sucesso' } 
      format.json { render :show, status: :created, location: @campaign } 
      else 
      format.html { render :new } 
      format.json { render json: @campaign.errors, status: :unprocessable_entity } 
      end 
     end 
    end 
private 

     def campaign_params 
      params.require(:campaign).permit(:name, :active, questions_attributes: [:id, :title, :image, :campaign_id]) 
     end 

Kampagne/_form erstellen. html.erb

<%= form_for @campaign, html: { multipart: true } do |f| %> 
    <%= f.label :name %> 
    <%= f.text_field :name %><br /> 

    <%= f.radio_button(:active, true) %> 
    <%= f.label :active, 'Ativado', :value => true %> 

    <%= f.radio_button(:active, false) %> 
    <%= f.label :active, 'Desativado', :value => false %> 

     <%= f.fields_for :questions, @questions_builded do |q| %> 
     <%= q.label :title %> 
     <%= q.text_field :title %><br /> 

     <%= q.label 'Imagem' %> 
     <%= q.file_field :image %><br /> 
     <% end %> 

    <%= f.submit %> 
<% end %> 

Wenn ich das Formular abgesendet haben, zeigt das Terminal diesen Fehler und nichts

Started POST "/campaigns" for 127.0.0.1 at 2017-05-12 14:23:45 -0300 
Processing by CampaignsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"tLnQyhz5bzs5B+REReoP10mnxTlCA+6MR1qFgL3D750AJy8L8g0RprtpweBCSP7uvwEmFwgqo+IG8b/gBbIUnQ==", "campaign"=>{"name"=>"qwqwqwwqqw", "active"=>"true", "questions_attributes"=>{"0"=>{"title"=>"popooppoop", "image"=>#<ActionDispatch::Http::UploadedFile:0xb52904a4 @tempfile=#<Tempfile:/tmp/RackMultipart20170512-10982-1c5tt8u.jpg>, @original_filename="Aluguel-de-carro-na-Grécia.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"campaign[questions_attributes][0][image]\"; filename=\"Aluguel-de-carro-na-Gr\xC3\xA9cia.jpg\"\r\nContent-Type: image/jpeg\r\n">}}}, "commit"=>"Create Campaign"} 
Command :: PATH=/usr/local/bin/:$PATH; file -b --mime '/tmp/30d1c81ad380893c238e4a9d175a478420170512-10982-1yj8kga.jpg' 
Command :: PATH=/usr/local/bin/:$PATH; identify -format '%wx%h,%[exif:orientation]' '/tmp/30d1c81ad380893c238e4a9d175a478420170512-10982-16bluom.jpg[0]' 2>/dev/null 
Command :: PATH=/usr/local/bin/:$PATH; identify -format %m '/tmp/30d1c81ad380893c238e4a9d175a478420170512-10982-16bluom.jpg[0]' 
Command :: PATH=/usr/local/bin/:$PATH; convert '/tmp/30d1c81ad380893c238e4a9d175a478420170512-10982-16bluom.jpg[0]' -auto-orient -resize "300x300>" '/tmp/8c747e6a91db0fc3579f3c740443c72120170512-10982-1e4nolk' 
Command :: PATH=/usr/local/bin/:$PATH; identify -format '%wx%h,%[exif:orientation]' '/tmp/30d1c81ad380893c238e4a9d175a478420170512-10982-16bluom.jpg[0]' 2>/dev/null 
Command :: PATH=/usr/local/bin/:$PATH; identify -format %m '/tmp/30d1c81ad380893c238e4a9d175a478420170512-10982-16bluom.jpg[0]' 
Command :: PATH=/usr/local/bin/:$PATH; convert '/tmp/30d1c81ad380893c238e4a9d175a478420170512-10982-16bluom.jpg[0]' -auto-orient -resize "100x100>" '/tmp/8c747e6a91db0fc3579f3c740443c72120170512-10982-13fpuuf' 
    (0.1ms) BEGIN 
Command :: PATH=/usr/local/bin/:$PATH; file -b --mime '/tmp/30d1c81ad380893c238e4a9d175a478420170512-10982-v99csi.jpg' 
    (0.1ms) ROLLBACK 
    Rendering campaigns/new.html.erb within layouts/application 
    Rendered campaigns/_form.html.erb (3.6ms) 
    Rendered campaigns/new.html.erb within layouts/application (4.5ms) 
    Rendered layouts/_header.html.erb (0.9ms) 
Completed 200 OK in 211ms (Views: 36.6ms | ActiveRecord: 0.2ms) 

ich die Bilder verwenden gem Büroklammer und gem ‚Büroklammer-cloudinary‘ zu Speicher erstellt wird.

PS: In Frage/_form.html.erb funktioniert

Antwort

1

Der Rollbacks wahrscheinlich darauf hinweisen, dass Ihr Modell nicht gültig ist, wenn Sie versuchen, es zu speichern. Dies ist eine Vermutung, aber vielleicht hat es mit der zu tun, weil die Kampagne nach der Frage gespeichert wird?

Für Zustandsprobleme wie diese mag ich einen Debugger oder Logging-Status in den Protokollen verwenden. Sie können beispielsweise eine Protokollanweisung hinzufügen, um zu überprüfen, ob das Modell gültig ist, bevor Sie versuchen, es im Controller zu speichern.

@campaign.valid?; logger.info(@campaign.errors.full_messages) 
+0

folgte ich Ihren Vorschlag und jetzt gibt diese Meldung: „[“ Fragen Kampagne muss vorhanden sein „‚Fragen Kampagne nicht leer sein‘]“ – Eduardorph

+0

Das bestätigt, dass die Fragen vor der Kampagne gespeichert werden und dass Der Speichervorgang schlägt aufgrund der 'validates_associated: campaign' im Question-Modell fehl. Wenn Sie diese Validierung beibehalten möchten, können Sie zunächst das Kampagnenmodell speichern und anschließend die Frage speichern. – Puhlze

+0

Ich habe die validates_associated: -Kampagne entfernt und @ campaign.questions.create (campaign_id: @ campaign.id) in if @ campaign.save eingefügt und funktioniert noch nicht. – Eduardorph