未允许的参数:assets_attributes

时间:2013-08-22 23:42:37

标签: ruby-on-rails ruby paperclip

Ruby版本: 2.0

Rails版本: 4.0

我找到了一百个与我的情况类似的问题,但没有一个答案对我有用。

我正在使用回形针并尝试将多张图片上传到article。我有一个assets模型来实现这一目标。

我已经在文章表单中嵌套了模型:

<%= form_for(@article, :html => { :multipart => true }) do |f| %>
  <% if @article.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@article.errors.count, "error") %> prohibited this article from being saved:</h2>

      <ul>
      <% @article.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :content %><br>
    <%= f.text_area :content %>
  </div>

  <%= f.fields_for :assets do |asset| %>
    <%= asset.file_field :image %><br />
  <% end %>

  <div class="actions">
    <%= f.submit %>
<% end %>

将模型和(我认为是)唯一的属性列入白名单 - image

def article_params
  params.require(:article).permit(:title, :content)
end

def asset_params
  params.require(:asset).permit(:image)
end

然而,当我尝试使用图像创建一篇新文章时,我仍然在我的开发日志中得到这个:

Unpermitted parameters: assets_attributes

我错过了什么? (下面的其他资源):

article.rb

class Article < ActiveRecord::Base
    has_many :assets
    accepts_nested_attributes_for :assets
end

asset.rb

class Asset < ActiveRecord::Base
    belongs_to :article

    has_attached_file :image, 
        :styles => {
            :thumb => '150x150#',
            :medium => '300x300>',
            :large => '600x600>'
        } 
end

从开发日志中完全转储

Started POST "/articles" for 127.0.0.1 at 2013-08-22 18:36:46 -0500
Processing by ArticlesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"8x5YJgZV7PsQkqxMM3nOvEZ5Zr4m7tKy8FAiQayvTrI=", "article"=>{"title"=>"Test", "content"=>"Test", "assets_attributes"=>{"0"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007fd3fbcf2bb0 @tempfile=#<Tempfile:/var/folders/ws/xtl93rn97nv1n1l61kzr9m0m0000gn/T/RackMultipart20130822-11705-evlabr>, @original_filename="PLunKmQ.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"article[assets_attributes][0][image]\"; filename=\"PLunKmQ.jpg\"\r\nContent-Type: image/jpeg\r\n">}}}, "commit"=>"Create Article"}
Unpermitted parameters: assets_attributes
  [1m[35m (0.1ms)[0m  begin transaction
  [1m[36mSQL (0.4ms)[0m  [1mINSERT INTO "articles" ("content", "created_at", "title", "updated_at") VALUES (?, ?, ?, ?)[0m  [["content", "Test"], ["created_at", Thu, 22 Aug 2013 23:36:46 UTC +00:00], ["title", "Test"], ["updated_at", Thu, 22 Aug 2013 23:36:46 UTC +00:00]]
  [1m[35m (0.8ms)[0m  commit transaction
Redirected to http://localhost:3000/articles/15
Completed 302 Found in 5ms (ActiveRecord: 1.3ms)

2 个答案:

答案 0 :(得分:3)

您需要允许嵌套assets_attributes通过article参数,因为assets_attributes是文章参数的一部分。

替换:

def article_params
  params.require(:article).permit(:title, :content)
end

def asset_params
  params.require(:asset).permit(:image)
end

使用:

def article_params
  params.require(:article).permit(:title, :content, assets_attributes: [:image])
end

答案 1 :(得分:1)

您需要将assets_attributes添加到允许的参数中,如下所示:

params.require(:article).permit(:title, :content, assets_attributes: [:id, :image] )

你可以将id保留,但你需要指定图像,它确实需要像我上面所示的数组