2017-05-13 19 views
0

以下のサーバーログで明示的に挿入するギャラリー属性を取得できますが、画像属性も挿入されません。Railsのネストされた属性は搬送波を使用して保存されません

サーバ応答

Started POST "/galleries" for 127.0.0.1 at 2017-05-13 18:19:23 +1000 
Processing by GalleriesController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"LACaMz44B9mn/psLYjzs8qrwo9mr0l2OEIPg+VmCn9CdbGhBh9rDUJ6FE0EOwKCj7aZVjbM4+t0YoaFIRX7IEA==", "gallery"=>{"name"=>"Hello", "cover"=>"123456", "picture"=>{"picture"=>#<ActionDispatch::Http::UploadedFile:0xb943d50 @tempfile=#<Tempfile:C:/Users/Lee/AppData/Local/Temp/RackMultipart20170513-2604-b2lnrz.jpg>, @original_filename="Skateboard 1.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"gallery[picture][picture]\"; filename=\"Skateboard 1.jpg\"\r\nContent-Type: image/jpeg\r\n">}}, "commit"=>"Create Gallery"} 
Unpermitted parameter: picture 
    (0.0ms) begin transaction 
    SQL (1.0ms) INSERT INTO "galleries" ("name", "cover", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "Hello"], ["cover", 123456], ["created_at", 2017-05-13 08:19:23 UTC], ["updated_at", 2017-05-13 08:19:23 UTC]] 
    (65.1ms) commit transaction 
Redirected to http://localhost:3000/ 
Completed 302 Found in 74ms (ActiveRecord: 66.1ms) 

Started GET "/" for 127.0.0.1 at 2017-05-13 18:19:23 +1000 
.... 

GalleriesController

class GalleriesController < ApplicationController 

    def new 
    @gallery = Gallery.new 
    end 

    def create 
    @gallery = Gallery.new(gallery_params) 
    if @gallery.save  
     flash[:success] = "Picture created!" 
     redirect_to root_url 
    else 
     render 'new' 
    end 
    end 

private 

    def gallery_params 
     params.require(:gallery).permit(:id, :name, :cover, pictures_attributes: [:id, :gallery_id, :picture, :_destroy]) 
     end 
    end 

_form.html.erb部分new.html.erb内からレンダリング

<%= form_for @gallery do |f| %> 
    <div class="field"> 
    <%= f.label :name %> 
    <%= f.text_field :name %> 
    </div> 
    <div class="field"> 
    <%= f.label :cover %> 
    <%= f.text_field :cover %> 
    </div> 
    <div id="pictures"> 
    <%= f.fields_for @gallery.pictures do |pic| %> 
     <%= pic.file_field :picture %> 
    </div> 
    <% end %> 
    <div id="submit"> 
    <%= f.submit %> 
    </div> 
<% end %> 

モデル、ギャラリー

class Gallery < ApplicationRecord 
    has_many :pictures 
    validates :name, presence: true 
    validates :cover, presence: true 
    accepts_nested_attributes_for :pictures, allow_destroy: true 
end 

ピクチャー

class Picture < ApplicationRecord 
    belongs_to :gallery 
    validates :gallery_id, presence: true 
    validates :picture, presence: true 
    mount_uploader :picture, PictureUploader 
    serialize :picture, JSON 
end 

移行、ギャラリー

class CreateGalleries < ActiveRecord::Migration[5.0] 
    def change 
    create_table :galleries do |t| 
     t.string :name 
     t.integer :cover 

     t.timestamps 
    end 
    end 
end 

ピクチャーここにあなたのパラメータラインから判断

class CreatePictures < ActiveRecord::Migration[5.0] 
    def change 
    create_table :pictures do |t| 
     t.integer :gallery_id 
     t.string :picture 

     t.timestamps 
    end 
    end 
end 

答えて

1

許可されていないパラメータにあなたの強いパラメータを変更する必要があります。絵

をごfields_forが間違っているため、エラーがあります。 の最初のパラメータfields_forは、record_name(あなたの場合は:pictures)である必要があります。

fields_for(のrecord_name、record_object =ゼロ、オプション= {}、&ブロック)

あなたは間違っparamsはをもたらすをもたらす最初のパラメータとしてrecord_objectを渡します許容されないエラー。コードを下に変更すると、問題が解決するはずです。

<%= f.fields_for :pictures, @gallery.pictures do |pic| %> 
    <%= pic.file_field :picture %> 
<% end %> 
+0

あなたの入力に感謝のパヴァン。あなたは正しいですが、私がこれをしたときに、画像の 'file_field'がページ上で消えてしまったので、' f.fields_for @ gallery.pictures do..'をしなければなりません。 "name:" pictures [picture] "とエラーを取り除きますが、これはピクチャではなくギャラリー属性を挿入するだけです。コントローラでこれを処理するには、 'params [:pictures] [:picture] .each do | pic |' '@picture = @ gallery.pictures.create(picture:pic)' '作品:) Thanks guys –

+0

実際にはエラーが発生しますが、代わりに@picture = @ gallery.pictures.create(画像:params [:pictures] [:picture]) 'が1つの画像 –

0

Parameters: {"utf8"=>"✓", "authenticity_token"=>"LACaMz44B9mn/psLYjzs8qrwo9mr0l2OEIPg+VmCn9CdbGhBh9rDUJ6FE0EOwKCj7aZVjbM4+t0YoaFIRX7IEA==", 
"gallery"=>{"name"=>"Hello", "cover"=>"123456", 
"picture"=>{"picture"=>#<ActionDispatch::Http::UploadedFile:0xb943d50 
@tempfile=#<Tempfile:C:/Users/Lee/AppData/Local/Temp/RackMultipart20170513-2604-b2lnrz.jpg>, 
@original_filename="Skateboard 1.jpg", @content_type="image/jpeg", 
@headers="Content-Disposition: form-data; name=\"gallery[picture][picture]\"; 
filename=\"Skateboard 1.jpg\"\r\nContent-Type: image/jpeg\r\n">}}, "commit"=>"Create Gallery"} 

と事実は、あなたは結果を取得している:Unpermitted parameter: pictureは、あなたが

def gallery_params 
    params.require(:gallery).permit(:id, :name, :cover, picture: [:id, :gallery_id, :picture, :_destroy]) 
end 
+0

はい、私はその絵の属性について言っていると思いますので間違いは別の場所にあるはずですか? –

+0

@LeeEather現在、パラメータハッシュ 'pictures_attributes'を許可していますが、パラメータhash 'picture'を送信しています。 – DevJem

+0

これを試してみましたが、私はGalleryの 'unknown attribute error picture'を取得しました。' Picture'モデル属性を追加しなくても、フォームのパラメータの 'picture_attributes'として写真の属性を指定しなければならないと確信しています。特にそれらを参照することはまだ動作しませんので、何か他のものでなければなりません –

関連する問題