Form with nested attribute and image upload

Asked

Viewed 40 times

0

I have the following models

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

I want to be able to create a QUESTION in the same form where I create a campaign

Follow the CAMPAIGN CONTROLLER

    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

Follow my FORM

<%= 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 %>

When I fill out the information and submit the form the terminal returns it to me:

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)

I don’t understand what could be wrong, in FORM QUESTION I can create without problems, but when I do for the campaign, no.

2 answers

0

tries to change the campaign_params to:

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

I don’t know exactly if that’s the question, but that’s how it’s working around here. :)

0

In Rails 5 I used optional: true belongs_to :company, optional: true

Browser other questions tagged

You are not signed in. Login or sign up in order to post.