2016-06-15 15 views
0

私はフォームを送信すると、それはimage_postが作成されますが、添付なし:画像失敗RoR:Carrierwaveとネストされたフォーム(ポリモーフィック)を使用して画像をアップロードできません。 .permit(:タイトル、attachments_attributes::画像)

(ポストを作成した)を提出image_postの

のparams { "タイトル": "こんにちは"、 "attachments_attributes":{ "0":{ "イメージ": "ではない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) 

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:

は( https://gorails.com/episodes/comments-with-polymorphic-associations Iは、多型の添付ファイルを作成するには、このビデオを使用しました)

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 

答えて

0

添付ファイルを送信する解決策が見つかりました。 nested attributes, passing current_user.id to nested model

コントローラの作成アクションでuserパラメータを渡す必要があります。やるrespond_to image_post.attachments.first.user = CURRENT_USER

@

image_post_controller.rb

@image_post = current_user.image_posts.build(image_post_params) |形式| ...

関連する問題