RoR:我无法使用Carrierwave和嵌套形式(多态)上传图像.permit(:title,attachments_attributes :: image)

时间:2016-06-15 18:13:55

标签: ruby-on-rails carrierwave

当我提交表单时,它会创建image_post但没有附加:image

提交失败的参数(创建帖子)

  

image_post {“title”:“Hello”,“attachments_attributes”:{“0”:{“image”:“不是JSON   可编码“}}}

Started POST "/image_posts" for 46.28.200.155 at 2016-06-15 17:29:18 +0000
Processing by ImagePostsController#create as HTML
  Parameters: {
    "utf8" => "✓",
    "authenticity_token" => "423LxTN6mPaa3fxwOsyoAvfK5HpdqivTUGEFlxLkLdgBDKKDjWtBUGc77UCu+9jJdjwtwHhzMGLxQ4EoimbPjQ==",
    "image_post" => {
      "title" => "Hello", "attachments_attributes" => {
        "0" => {
          "image" => [# < ActionDispatch::Http::UploadedFile: 0x00000006377818 @tempfile = # < Tempfile: /tmp/RackMultipart
            20160615 - 218991 - 1 t6an3e.jpg > , @original_filename = "1465056537941.jpg", @content_type = "image/jpeg", @headers = "Content-Disposition: form-data; name=\"image_post[attachments_attributes][0][image][]\"; filename=\"1465056537941.jpg\"\r\nContent-Type: image/jpeg\r\n" >
          ]
        }
      }
    },
    "button" => ""
  }
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
Unpermitted parameter: image
   (2.4ms)  BEGIN
  SQL (7.1ms)  INSERT INTO "posts" ("type", "title", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["type", "ImagePost"], ["title", "Hej"], ["user_id", 1], ["created_at", "2016-06-15 17:29:18.844844"], ["updated_at", "2016-06-15 17:29:18.844844"]]
   (3.3ms)  COMMIT
Redirected to https://oanstein-app-lowryder.c9users.io/posts/23-hej
Completed 302 Found in 43ms (ActiveRecord: 13.2ms)

(我用这个视频创建了多态附件: https://gorails.com/episodes/comments-with-polymorphic-associations

image_posts / form.html.haml

= form_for @image_post, html: { class: "ui form", multipart: true } do |f|
  .field
    = f.label :title, 'Titel'
    = f.text_field :title, placeholder: 'Titel', required: true
  .field.ui.secondary.segment
    = f.fields_for :attachments do |a|
      = a.file_field :image, multiple: true 
  = button_tag 'Post erstellen', class: 'ui blue button'

image_post.rb和post.rb

class ImagePost < Post
end

class Post < ActiveRecord::Base
  belongs_to :user
  has_many :attachments, as: :attachable
  accepts_nested_attributes_for :attachments, reject_if: :all_blank
...
end

attachment.rb

class Attachment < ActiveRecord::Base
  belongs_to :attachable, polymorphic: true
  belongs_to :user

  mount_uploader :image, ImageUploader
  validates_integrity_of  :image
  validates_processing_of :image
end

image_post_controller.rb

class ImagePostsController < ApplicationController
  before_action :authenticate_user!, only: [:create, :destroy]
  before_action :set_image_post, only: [:edit, :update]
  before_action :ensure_admin!, only: :destroy

  def new
    @image_post = ImagePost.new
    @image_post.attachments.build
  end

  def create
    @image_post = current_user.image_posts.build(image_post_params)

    respond_to do |format|
      if @image_post.save
        format.html { redirect_to post_path(@image_post), 
                      notice: 'Post erstellt.' }
        format.json { render :show, status: :created, location: @image_post }
      else
        format.html { render :new }
        format.json { render json: @image_post.errors, 
                             status: :unprocessable_entity }
      end
    end
  end
...

  private
    def image_post_params
      params.require(:image_post).permit(:title, attachments_attributes: :image)
    end
end

1 个答案:

答案 0 :(得分:0)

我找到了提交附件的解决方案。 nested attributes, passing current_user.id to nested model

我必须在我的控制器的创建动作中传递用户参数。

image_post_controller.rb

  

@image_post = current_user.image_posts.build(image_post_params)    @ image_post.attachments.first.user = current_user

     

respond_to do | format | ...