未允许的参数:photos_attributes

时间:2014-10-18 16:52:53

标签: ruby-on-rails paperclip

我试图将Paperclip添加到Forem-based Rails app

直播应用:http://runnable.com/VEEnRTEK-igg4XGD/forem-with-paperclip-for-ruby-on-rails

但为什么我上传后会Unpermitted parameters: photos_attributes

photo.rb

class Photo < ActiveRecord::Base
  belongs_to :post, :class_name => "Forem::Post"
  has_attached_file :attachment, :styles => {
    :medium => "300x300>",
    :thumb => "100x100>"
  }
  validates_attachment_presence :attachment
end 

decorators/controllers/forem/topics_controller_decorator.rb

Forem::TopicsController.class_eval do
  def new
    authorize! :create_topic, @forum
    @topic = @forum.topics.build
    post = @topic.posts.build
    post.photos.build
  end
end

decorators/controllers/forem/posts_controller_decorator.rb

Forem::PostsController.class_eval do
  def create
    @post = @topic.posts.new(post_params)
    @post.user = forem_user
  end

protected
  def post_params
    params.require(:post).permit(:text, :password, :email, :photos_attributes)
  end
end

decorators/models/forem/posts_decorator.rb

Forem::Post.class_eval do
  has_many :photos

  accepts_nested_attributes_for :photos
end 

app/views/forem/posts/_post.html.erb

<% for photo in post.photos %>
  <%= image_tag photo.attachment.url(:medium) %>
<% end %>

app/views/forem/posts/_form.html.erb

<%= f.simple_fields_for :photos do |photo| %>
  <%= render :partial => "photos/form", :locals => { :f => photo }%>
<% end %>

app/views/photos/_form.html.erb

<%= f.file_field :attachment, :id => "photo_attachment" %> 

输出:

Started POST "/default/topics" at 2014-10-17 17:35:28 +0000
Processing by Forem::TopicsController#create as HTML
Parameters: {"utf8"=>"?", "authenticity_token"=>"rCLEcuRvkWU8Jsr+cEjtC1tTF97JGx/9ii54Gw9H42E=", "topic"=>{"subject"=>"asdasdasda", "posts_attributes"=>{"0"=>{"text"=>"dasdasdasd", "photos_attributes"=>{"0"=>{"attachment"=>#<ActionDispatch::Http::UploadedFile:0x0000000490f048 @tempfile=#<File:/tmp/RackMultipart20141017-634-1by49l4>, @original_filename="download.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"topic[posts_attributes][0][photos_attributes][0][attachment]\"; filename=\"download.jpg\"\r\nContent-Type: image/jpeg\r\n">}}}}}, "commit"=>"Create Topic", "forum_id"=>"default"}
[1m[36mUser Load (0.3ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1[0m
[1m[35mForem::Forum Load (0.2ms)[0m SELECT "forem_forums".* FROM "forem_forums" WHERE "forem_forums"."slug" = 'default' ORDER BY "forem_forums"."name" ASC LIMIT 1
[1m[36mForem::Category Load (0.1ms)[0m [1mSELECT "forem_categories".* FROM "forem_categories" WHERE "forem_categories"."id" = ? LIMIT 1[0m [["id", 1]]
Unpermitted parameters: photos_attributes
[1m[35m (0.1ms)[0m begin transaction
[1m[36mForem::Topic Exists (0.2ms)[0m [1mSELECT 1 AS one FROM "forem_topics" WHERE ("forem_topics"."id" IS NOT NULL) AND "forem_topics"."slug" = 'asdasdasda' LIMIT 1[0m
[1m[35mSQL (0.4ms)[0m INSERT INTO "forem_topics" ("created_at", "forum_id", "slug", "subject", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?) [["created_at", "2014-10-17 17:35:28.968381"], ["forum_id", 1], ["slug", "asdasdasda"], ["subject", "asdasdasda"], ["updated_at", "2014-10-17 17:35:28.968381"], ["user_id", 1]]
[1m[36mSQL (0.2ms)[0m [1mINSERT INTO "forem_posts" ("created_at", "text", "topic_id", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?)[0m [["created_at", "2014-10-17 17:35:28.970016"], ["text", "dasdasdasd"], ["topic_id", 15], ["updated_at", "2014-10-17 17:35:28.970016"], ["user_id", 1]]
[1m[35mForem::Topic Load (0.0ms)[0m SELECT "forem_topics".* FROM "forem_topics" WHERE "forem_topics"."id" = ? LIMIT 1 [["id", 15]]
[1m[36mSQL (0.3ms)[0m [1mUPDATE "forem_topics" SET "last_post_at" = '2014-10-17 17:35:28.970016' WHERE "forem_topics"."id" = 15[0m
[1m[35mForem::Subscription Load (0.1ms)[0m SELECT "forem_subscriptions".* FROM "forem_subscriptions" WHERE "forem_subscriptions"."topic_id" = ? ORDER BY "forem_subscriptions"."id" ASC LIMIT 1000 [["topic_id", 15]]
[1m[36mSQL (0.1ms)[0m [1mUPDATE "forem_posts" SET "notified" = 't' WHERE "forem_posts"."id" = 15[0m
[1m[35mSQL (0.1ms)[0m UPDATE "forem_posts" SET "state" = 'approved' WHERE "forem_posts"."id" = 15
[1m[36m (0.1ms)[0m [1mSELECT COUNT(*) FROM "forem_subscriptions" WHERE "forem_subscriptions"."topic_id" = ? AND "forem_subscriptions"."subscriber_id" = 1[0m [["topic_id", 15]]
[1m[35mSQL (0.1ms)[0m INSERT INTO "forem_subscriptions" ("subscriber_id", "topic_id") VALUES (?, ?) [["subscriber_id", 1], ["topic_id", 15]]
[1m[36mSQL (0.1ms)[0m [1mUPDATE "forem_topics" SET "state" = 'approved' WHERE "forem_topics"."id" = 15[0m
[1m[35m (8.0ms)[0m commit transaction
Redirected to http://web-025a9cdf-c1cd-4f15-b7e7-54558afbdf6c.runnable.com/default/topics/asdasdasda
Completed 302 Found in 123ms (ActiveRecord: 10.7ms)

1 个答案:

答案 0 :(得分:2)

这个答案只包括你的问题。你的错误是控制器名称。创建主题由Forem :: TopicsController处理。这是更正版本。

#app/decorators/controllers/forem/topics_controller.rb
Forem::TopicsController.class_eval do
  def new
    authorize! :create_topic, @forum
    @topic = @forum.topics.build
    post = @topic.posts.build
    post.photos.build
  end
  def create
    @topic = @forum.topics.new(topic_params)
    if @topic.save
      redirect_to @topic
    else
      render :new
    end
  end

  protected
    def topic_params
      params.require(:topic).permit(:subject, :posts_attributes => [ :text, {:photos_attributes => [:attachment]} ])
    end
end

第二个错误是回形针本身。版本4中的回形针需要内容类型验证。这是更正的照片模型。

#app/models/photo.rb
class Photo < ActiveRecord::Base
  belongs_to :post, :class_name => "Forem::Post"
  has_attached_file :attachment, :styles => {
    :medium => "300x300>",
    :thumb => "100x100>"
  }
  validates_attachment :attachment, presence: true, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] }
end

在您的服务器上检查它,因为runnable没有安装ImageMagick,这是回形针图像附件所必需的。