带有图像上传的嵌套属性

Nested Attributes with image 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

我想在创建 CAMPAIGN 的同一个_form 中创建一个问题

活动控制者

    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

campaign/_form.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 %>

当我提交表单时,终端显示此错误并且没有创建任何内容

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)

我使用gem回形针和gem'paperclip-cloudinary'来存储图像。

PS:在 question/_form.html.erb 中工作正常

回滚可能表明您的模型在您尝试保存时无效。这是一个猜测,但可能与 validates_associated :campaign 有关,因为在问题之后正在保存活动?

对于像这样的状态问题,我喜欢使用调试器或在日志中记录状态。例如,您可以添加一个日志语句来检查模型是否有效,然后再尝试将其保存在控制器中。

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