Rails 4:Paperclip错误发布模型缺少attr_accessor需要'image_file_name'

时间:2015-10-06 15:36:40

标签: ruby-on-rails ruby-on-rails-4 heroku paperclip attr-accessor

首先,让我指出我确实在Stack Overflow上找到了两个与我的问题非常相似的问题:

但是,我不确定问题是否相同。

让我解释一下。

在我的Rails 4应用程序中,我有以下模型:

class User < ActiveRecord::Base
  has_many :administrations
  has_many :calendars, through: :administrations
end

class Calendar < ActiveRecord::Base
  has_many :administrations
  has_many :users, through: :administrations
  has_many: :posts
end

class Administration < ActiveRecord::Base
  belongs_to :user
  belongs_to :calendar
end

class Post < ActiveRecord::Base
  belongs_to :calendar
end

Post模型具有以下属性:

references :calendar, index: true, foreign_key: true
date :date
time :time
string :subject
string :format
text :copy
attachment :image

并使用Post模型中的Paperclip设置attachment,如下所示:

has_attached_file :image, styles: { small: "64x64", med: "100x100", large: "200x200" }
validates_attachment :image, :content_type => { :content_type => "image/png" },
                                :size => { :in => 0..3000.kilobytes }

以下是我使用的迁移:

class AddPaperclipToPost < ActiveRecord::Migration
  def change
    remove_column :posts, :media
    add_attachment :posts, :image

  end
end

注意remove_column :posts, :media在这里,因为在安装Paperclip之前,我曾经有一个名为“media”的简单属性。这不是一个功能文件字段,而是一个虚假的占位符,我用来模拟我的表并让它出现在我的UI中。

- - - -

更新:我检查了schema.rb文件并且迁移确实完成了,因为我收到了以下Post表:

create_table "posts", force: :cascade do |t|
  t.integer  "calendar_id"
  t.date     "date"
  t.time     "time"
  t.string   "subject"
  t.string   "format"
  t.text     "copy"
  t.datetime "created_at",         null: false
  t.datetime "updated_at",         null: false
  t.string   "image_file_name"
  t.string   "image_content_type"
  t.integer  "image_file_size"
  t.datetime "image_updated_at"
end

-----

这是我不明白的地方:

- 当我在本地运行应用程序时:一切正常,我可以创建新帖子,上传文件,保存帖子,然后在帖子显示视图中查看。

- 当我部署到Heroku时:我可以创建一个新帖子并上传文件,但是当我尝试保存它时,我收到错误消息We're sorry, but something went wrong. If you are the application owner check the logs for more information.

所以,我得到并检查日志,并获得以下信息:

2015-10-06T15:16:00.867374+00:00 app[web.1]: Started GET "/calendars" for 24.205.62.204 at 2015-10-06 15:16:00 +0000
2015-10-06T15:16:00.874619+00:00 app[web.1]:   User Load (1.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
2015-10-06T15:16:00.886324+00:00 app[web.1]:   Administration Load (1.2ms)  SELECT  "administrations".* FROM "administrations" WHERE "administrations"."calendar_id" = $1 AND "administrations"."user_id" = $2  ORDER BY "administrations"."id" ASC LIMIT 1  [["calendar_id", 1], ["user_id", 1]]
2015-10-06T15:16:00.889529+00:00 app[web.1]:   Rendered calendars/index.html.erb within layouts/calendars (11.8ms)
2015-10-06T15:16:00.871085+00:00 app[web.1]: Processing by CalendarsController#index as HTML
2015-10-06T15:16:00.880653+00:00 app[web.1]:   Calendar Load (1.4ms)  SELECT "calendars".* FROM "calendars" INNER JOIN "administrations" ON "calendars"."id" = "administrations"."calendar_id" WHERE "administrations"."user_id" = $1  [["user_id", 1]]
2015-10-06T15:16:00.888807+00:00 app[web.1]:   Administration Load (1.3ms)  SELECT  "administrations".* FROM "administrations" WHERE "administrations"."user_id" = $1 AND "administrations"."calendar_id" = $2 LIMIT 1  [["user_id", 1], ["calendar_id", 1]]
2015-10-06T15:16:00.890698+00:00 app[web.1]: Completed 200 OK in 20ms (Views: 10.1ms | ActiveRecord: 5.2ms)
2015-10-06T15:16:00.894553+00:00 heroku[router]: at=info method=GET path="/calendars" host=calendy.herokuapp.com request_id=84199c51-e02b-43a1-8bf8-ac14b102146d fwd="24.205.62.204" dyno=web.1 connect=0ms service=26ms status=200 bytes=2728
2015-10-06T15:16:02.887552+00:00 heroku[router]: at=info method=GET path="/calendars/1" host=calendy.herokuapp.com request_id=3ebd4dd2-ecad-4bef-8bdc-ac3f61a807ac fwd="24.205.62.204" dyno=web.1 connect=1ms service=66ms status=200 bytes=8538
2015-10-06T15:16:02.826123+00:00 app[web.1]: Started GET "/calendars/1" for 24.205.62.204 at 2015-10-06 15:16:02 +0000
2015-10-06T15:16:02.858126+00:00 app[web.1]:   Post Exists (3.9ms)  SELECT  1 AS one FROM "posts" WHERE "posts"."calendar_id" = $1 LIMIT 1  [["calendar_id", 1]]
2015-10-06T15:16:02.834055+00:00 app[web.1]:   User Load (1.7ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
2015-10-06T15:16:02.883375+00:00 app[web.1]: Completed 200 OK in 54ms (Views: 37.0ms | ActiveRecord: 8.3ms)
2015-10-06T15:16:02.828856+00:00 app[web.1]: Processing by CalendarsController#show as HTML
2015-10-06T15:16:02.828863+00:00 app[web.1]:   Parameters: {"id"=>"1"}
2015-10-06T15:16:02.839230+00:00 app[web.1]:   Calendar Load (1.4ms)  SELECT  "calendars".* FROM "calendars" INNER JOIN "administrations" ON "calendars"."id" = "administrations"."calendar_id" WHERE "administrations"."user_id" = $1 AND "calendars"."id" = $2 LIMIT 1  [["user_id", 1], ["id", 1]]
2015-10-06T15:16:02.831152+00:00 app[web.1]:   Calendar Load (1.3ms)  SELECT  "calendars".* FROM "calendars" WHERE "calendars"."id" = $1 LIMIT 1  [["id", 1]]
2015-10-06T15:16:02.877428+00:00 app[web.1]:   Rendered calendars/show.html.erb within layouts/calendars (34.0ms)
2015-10-06T15:16:44.490246+00:00 app[web.1]: Started POST "/calendars/1/posts" for 24.205.62.204 at 2015-10-06 15:16:44 +0000
2015-10-06T15:16:44.498537+00:00 app[web.1]:    (1.2ms)  BEGIN
2015-10-06T15:16:44.501575+00:00 app[web.1]: Completed 500 Internal Server Error in 8ms (ActiveRecord: 3.8ms)
2015-10-06T15:16:44.493243+00:00 app[web.1]:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"j7RmblwHH2Aj0JCUrYKhAvVvdIDiSOY7DO07NbJNQM/OEsDzA70+jTABthyPO7/oZhCgb8+E/0DRhPdxO5WXIQ==", "post"=>{"date(1i)"=>"2015", "date(2i)"=>"10", "date(3i)"=>"6", "time(1i)"=>"2015", "time(2i)"=>"10", "time(3i)"=>"6", "time(4i)"=>"15", "time(5i)"=>"16", "subject"=>"Launch", "format"=>"Image", "copy"=>"Hey, Calendy is launching, take a look here!", "image"=>#<ActionDispatch::Http::UploadedFile:0x007fc6b1a6aa20 @tempfile=#<Tempfile:/tmp/RackMultipart20151006-6-ljl7ny.png>, @original_filename="calendy-social-media-calendar-tool-collaboration.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"post[image]\"; filename=\"calendy-social-media-calendar-tool-collaboration.png\"\r\nContent-Type: image/png\r\n">}, "commit"=>"Create Post", "calendar_id"=>"1"}
2015-10-06T15:16:44.502893+00:00 app[web.1]: 
2015-10-06T15:16:44.501140+00:00 app[web.1]:    (1.1ms)  ROLLBACK
2015-10-06T15:16:44.495949+00:00 app[web.1]:   Calendar Load (1.4ms)  SELECT  "calendars".* FROM "calendars" WHERE "calendars"."id" = $1 LIMIT 1  [["id", 1]]
2015-10-06T15:16:44.493113+00:00 app[web.1]: Processing by PostsController#create as HTML
2015-10-06T15:16:44.502896+00:00 app[web.1]: Paperclip::Error (Post model missing required attr_accessor for 'image_file_name'):
2015-10-06T15:16:44.502898+00:00 app[web.1]:   app/controllers/posts_controller.rb:30:in `create'
2015-10-06T15:16:44.502900+00:00 app[web.1]: 
2015-10-06T15:16:44.502901+00:00 app[web.1]: 
2015-10-06T15:16:44.784398+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=calendy.herokuapp.com request_id=46565200-4c91-4251-9bc3-bf8167c515c8 fwd="24.205.62.204" dyno=web.1 connect=1ms service=8ms status=304 bytes=93
2015-10-06T15:16:44.505034+00:00 heroku[router]: at=info method=POST path="/calendars/1/posts" host=calendy.herokuapp.com request_id=214a9ce9-deca-4ef1-954a-08b741c712fe fwd="24.205.62.204" dyno=web.1 connect=1ms service=129ms status=500 bytes=1714
updating Heroku CLI...done. Updated to 3.42.15

Paperclip::Error (Post model missing required attr_accessor for 'image_file_name'):似乎是问题,我不明白,因为我的posts_controller中有以下行:

def post_params
  params.require(:post).permit(:date, :time, :subject, :format, :copy, :image)
end

根据Paperclip documentation

的建议

我是否还应授权所有Paperclip属性,即:

  • image_file_name
  • image_file_size
  • image_content_type
  • image_updated_at

或者这是迁移的问题,因为它似乎是在我在本问题开头的参考文献中提到的另外两个Stack Overflo问题?

1 个答案:

答案 0 :(得分:1)

尝试以下方法:

def post_params
  params.require(:post).permit(:date, :time, :subject, :format, :copy, image: [:image_file_name, :image_file_size, :image_content_type, :image_updated_at])
end