问题
First, let me specify that I did find two questions on Stack Overflow that are very similar to my problem:
- Model missing required attr_accessor for 'image_file_name' - Ruby on Rails 4
- Paperclip Error: model missing required attr_accessor for 'avatar_file_name'
However, I am not sure the issue is the same.
Let me explain.
In my Rails 4 app, I have the following models:
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
The Post
model has the following attributes:
references :calendar, index: true, foreign_key: true
date :date
time :time
string :subject
string :format
text :copy
attachment :image
and the attachment
was setup as follows with Paperclip in Post model:
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 }
Here is the migration I used:
class AddPaperclipToPost < ActiveRecord::Migration
def change
remove_column :posts, :media
add_attachment :posts, :image
end
end
Note: remove_column :posts, :media
is here because I used to have a simple attribute named "media" before I installed Paperclip. This was a not a functionning file field, but a fake placeholder I used to modelize my table and have it appear in my UI.
—————
UPDATE: I checked my schema.rb file and the migration did go through, since I get the following 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
—————
Here is what I don't understand:
— When I run the app locally: everything works fine, I can create a new post, upload a file, save the post and then see it in the post show view.
— When I deploy to Heroku: I can create a new post and upload a file, but when I try to save it, I get the error message We're sorry, but something went wrong. If you are the application owner check the logs for more information.
So, I got and check the logs, and get the following info:
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
The line Paperclip::Error (Post model missing required attr_accessor for 'image_file_name'):
seems to be the problem, which I don't understand, since I have the following line in my posts_controller
:
def post_params
params.require(:post).permit(:date, :time, :subject, :format, :copy, :image)
end
as recommended in the Paperclip documentation.
Should I also authorize all Paperclip attributes, ie:
image_file_name
image_file_size
image_content_type
image_updated_at
Or is it a problem of migrations, as it seems to be in the other two Stack Overflo questions I mentioned in reference at the beginning of this question?
回答1:
Try the following:
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
来源:https://stackoverflow.com/questions/32974036/rails-4-paperclip-error-post-model-missing-required-attr-accessor-for-image-fi