Rails 4:回形针错误 Post 'image_file_name' 需要 attr_accessor 模型
Rails 4: Paperclip Error Post model missing required attr_accessor for 'image_file_name'
首先,让我说明一下,我确实在 Stack Overflow 上找到了两个与我的问题非常相似的问题:
- Model missing required attr_accessor for 'image_file_name' - Ruby on Rails 4
- Paperclip Error: model missing required attr_accessor for 'avatar_file_name'
但是,我不确定问题是否相同。
让我解释一下。
在我的 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
并且 attachment
在 Post 模型中使用回形针设置如下:
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"。这不是一个正常运行的文件字段,而是一个假的占位符,我用来建模我的 table 并让它出现在我的 UI.
中
——————
更新:我检查了我的 schema.rb 文件,迁移确实完成了,因为我得到以下 Post table:
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
——————
以下是我不明白的地方:
— 当我在本地 运行 应用程序时: 一切正常,我可以创建一个新的 post,上传一个文件,保存 post 然后在 post 显示视图中看到它。
— 当我部署到 Heroku 时: 我可以创建一个新的 post 并上传一个文件,但是当我尝试保存它时,我收到错误消息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" = 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" = AND "administrations"."user_id" = 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" = [["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" = AND "administrations"."calendar_id" = 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" = 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" = 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" = AND "calendars"."id" = 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" = 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" = 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 属性,即:
image_file_name
image_file_size
image_content_type
image_updated_at
或者是迁移的问题,就像我在这个问题开头提到的其他两个 Stack Overflo 问题一样?
尝试以下操作:
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
首先,让我说明一下,我确实在 Stack Overflow 上找到了两个与我的问题非常相似的问题:
- Model missing required attr_accessor for 'image_file_name' - Ruby on Rails 4
- Paperclip Error: model missing required attr_accessor for 'avatar_file_name'
但是,我不确定问题是否相同。
让我解释一下。
在我的 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
并且 attachment
在 Post 模型中使用回形针设置如下:
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"。这不是一个正常运行的文件字段,而是一个假的占位符,我用来建模我的 table 并让它出现在我的 UI.
——————
更新:我检查了我的 schema.rb 文件,迁移确实完成了,因为我得到以下 Post table:
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
——————
以下是我不明白的地方:
— 当我在本地 运行 应用程序时: 一切正常,我可以创建一个新的 post,上传一个文件,保存 post 然后在 post 显示视图中看到它。
— 当我部署到 Heroku 时: 我可以创建一个新的 post 并上传一个文件,但是当我尝试保存它时,我收到错误消息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" = 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" = AND "administrations"."user_id" = 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" = [["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" = AND "administrations"."calendar_id" = 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" = 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" = 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" = AND "calendars"."id" = 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" = 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" = 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 属性,即:
image_file_name
image_file_size
image_content_type
image_updated_at
或者是迁移的问题,就像我在这个问题开头提到的其他两个 Stack Overflo 问题一样?
尝试以下操作:
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