0

Tenho os seguintes 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

Quero poder criar uma QUESTION no mesmo form onde eu crio uma campanha

Segue o 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

Segue meu 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 %>

Quando eu preencho as informações e submeto o form o terminal me retorna isso:

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)

Não estou entendendo o que pode estar errado, no FORM da QUESTION eu consigo criar sem problemas, mas quando faço pela campanha, não.

2 Respostas 2

0

tenta alterar o campaign_params para:

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

Não sei exatamente se é a questão, mas é como está funcionando por aqui. :)

0

No rails 5 utilizei optional: true belongs_to :company, optional: true

0

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .