1

Ruby Version: 2.0

Rails Version: 4.0

I have found a hundred questions with similar situations to mine, but none of the answers seem to work for me.

I am using paperclip and attempting to upload multiple images to an article. I have an assets model to accomplish this.

I have nested the model inside the article form here:

<%= 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 %>

And whitelisted the model and (what I assume to be) its only attribute - image

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

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

Yet I am still getting this in my development log when I try to create a new article with an image:

Unpermitted parameters: assets_attributes

What am I missing? (additional resources below):

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

full dump from development log

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)
4

2 回答 2

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
于 2013-08-22T23:49:42.437 回答
1

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

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

您可能可以省略 id,但您确实需要指定图像,并且它确实需要像我上面显示的那样位于数组中

于 2013-08-22T23:49:11.693 回答