使用Paperclip的has_many附件的强参数嵌套表单

时间:2014-03-03 01:33:42

标签: ruby-on-rails ruby paperclip

我有一个带有以下型号的rails 4应用程序:

section.rb

belongs_to :page
belongs_to :user

has_many :assets, :dependent => :destroy
accepts_nested_attributes_for :assets, allow_destroy: true

asset.rb

belongs_to :section

has_attached_file :avatar, :styles => { :medium => "300x300>",
    :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"

和... sections_controller.rb

def create
  @section = @page.sections.new(section_params)
  @section.user = current_user

  if @section.save
    redirect_to page_sections_url(@page), notice: "'#{@section.name}' has been created successfully."
  else
    flash[:alert] =  "Uh oh! Looks like something went wrong" 
    render 'index'  
  end
end

所以我的问题......我怎样才能为此实现强参数?我将以下参数显示为输出:

{"utf8"=>"✓", "authenticity_token"=>"......=", "section"=>{"assets_attributes"=>{"0"=>{"attachment"=>#, @original_filename="calculator.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"section[assets_attributes][0][attachment]\"; filename=\"calculator.png\"\r\nContent-Type: image/png\r\n">}}, "name"=>"Foo", "content_md"=>"Bar"}, "commit"=>"Create Section", "action"=>"create", "controller"=>"sections", "page_id"=>"about"}

以及以下错误:Unpermitted parameters: assets_attributes ..当我将assets_attributes: {}追加到列表中时,我得到Unpermitted parameters: attachment ..但我该如何进行嵌套?

编辑:忘记了视图

<%= simple_form_for [@page, @section], html: {multipart: true , class: 'form-horizontal inplace-new '} do |f| %>

  <%= f.simple_fields_for :assets do |asset_fields| %>
        <%= asset_fields.file_field :attachment %>

另外.. asset_fields.input :attachment, as: :file不起作用.. file_field但是我做错了吗?

1 个答案:

答案 0 :(得分:0)

section_params的实现中添加以下内容

params.require(:sections).permit(
  :utf8,
  :authenticity_token,
  :section [
    :assets
  ],etc...)