2017-05-13 39 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 =零,選項= {},&塊)

你傳入record_object作爲第一參數,其中,導致錯PARAMS並導致未經許可的錯誤。將代碼更改爲下面的內容可以解決問題。

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

感謝pavan的輸入。你是對的,但是當我這樣做了'file_field'在頁面上的圖片自敗,所以我必須做'f.fields_for @ gallery.pictures do..'現在我意識到,以獲得參數允許我需要命名字段作爲'名稱:'圖片[圖片]「'它擺脫了錯誤,但這只是插入畫廊屬性而不是圖片。 (:pic圖片)''end'和所有[:圖片] | | PIC''然後= @picture @ gallery.pictures.create [圖片]。每次這樣做控制器我需要添加'PARAMS處理這個問題工程:)謝謝你們 –

+0

對不起實際上是產生一個錯誤,而不是它只是'@picture = @ gallery.pictures.create(圖片:PARAMS [:圖片] [:圖片])'因爲它是一個單一的圖片 –

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',但你提交參數哈希'圖片'。 – DevJem

+0

我試過了,我得到了一個'未知的屬性錯誤圖片庫'我很確定你必須指定你的圖片屬性作爲'picture_attributes'作爲參數從表單中添加'Picture'模型屬性而不需要特別是引用它們仍然不起作用,所以一定是別的東西 –