2014-02-18 96 views
1
class Document < ActiveRecord::Base 

    belongs_to :user 

    has_many :attachments 
    accepts_nested_attributes_for :attachments 

end 

class DocumentsController < ApplicationController 

    def new 
    @document = get_user.documents.build 
    3.times { @document.attachments.build } 
    end 

    def create 
    @document = Document.new post_params 
    end 

    def post_params 
    params.require(:document).permit(:id, :subject, :body, attachments_attributes:[:attachment]) 
    end 

end 

_form.html.slim嵌套屬性與軌道4個強參數

= f.fields_for :attachments do |builder| 
    = builder.file_field :attachment, multiple: true 

這裏的問題是:

的附件上我post_params是空的:

{"id"=>"", "to"=>"5621", "subject"=>"Hello", "body"=>"World", "attachments_attributes"=>{"0"=>{}}} 

回答

1

看起來這是做它的方式:

def post_params 
    params.require(:document).permit(:id, :subject, :body, attachments_attributes:[:attachment => []]) 
    end 
0

它看起來您可能會在表單標記上缺少multipart: true

+0

這是沒有問題的 – Dex

0

可能

def post_params 
    params.require(:document).permit(:id, :subject, :body, 
            attachments_attributes: ['0']) 
end 
+0

但也有附件無限量的。 – Dex