Rails 4:回形针错误Post模型缺少attr_accessor for'image_file_name' [英] Rails 4: Paperclip Error Post model missing required attr_accessor for 'image_file_name'

查看:186
本文介绍了Rails 4:回形针错误Post模型缺少attr_accessor for'image_file_name'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,让我指定我在Stack Overflow上发现了两个与我的问题非常相似的问题:



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



让我解释一下。



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

  class User< ActiveRecord :: Base 
has_many:管理
has_many:日历,通过::管理
结束

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

class Administration< ActiveRecord :: Base
belongs_to:用户
belongs_to:日历
结束

类Post< ActiveRecord :: Base
belongs_to:日历
结束



Post model具有以下属性:

 参考:calendar,index:true,foreign_key: true 
日期:日期
时间:时间
字符串:主题
字符串:格式
文本:复制
附件:图片

附件设置如下Post Post模式中的Paperclip: p>

  has_attached_file:图片,样式:{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
code>

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

>

更新:我检查了我的schema.rb文件,并且迁移经历了,因为我得到以下Post表:

  create_tableposts,force::cascade do | t | 
t.integercalendar_id
t.datedate
t.timetime
t.stringsubject
t.stringformat
t.textcopy
t.datetimecreated_at,null:false
t.datetimeupdated_at,null:false
t.stringimage_file_name
t.stringimage_content_type
t.integerimage_file_size
t.datetimeimage_updated_at
end

-----



这是我不明白的:



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

- 当我部署到Heroku时:我可以创建一个新帖子并上传一个文件,但是当我尝试保存它时,错误消息很抱歉,但出错了。如果您是应用程序所有者,请查看日志以获取更多信息。



因此,我得到并检查日志,并获取以下信息:

  2015-10-06T15:16:00.867374 + 00:00 app [web.1]:开始GET/日历 for 24.205.62.204 at 2015-10-06 15:16:00 +0000 
2015-10-06T15:16:00.874619 + 00:00 app [web.1]:用户负载(1.4ms)SELECTusers 。* FROMusersWHEREusers。id= $ 1 ORDER BYusers。idASC LIMIT 1 [[id,1]]
2015-10-06T15:16: 00.886324 + 00:00 app [web.1]:管理负载(1.2ms)SELECTadministrations。* FROMadministrationsWHEREadministrations。calendar_id= $ 1 ANDadministrations。user_id= $ 2 ORDER BY 管理。idASC LIMIT 1 [[calendar_id,1],[user_id,1]]
2015-10-06T15:16:00.889529 + 00:00 app [web.1] :在布局/日历中呈现日历/ index.html.erb(11.8ms)
2015-10-06T15:16:00.871085 + 00:00应用[web.1]:通过CalendarsController处理#index为HTML
2015-10-06T 15:16:00.880653 + 00:00 app [web.1]:日历加载(1.4ms)选择日历*从日历内部加入管理开启日历。id=管理。 calendar_idWHEREadministrations。user_id= $ 1 [[user_id,1]]
2015-10-06T15:16:00.888807 + 00:00 app [web.1]:Administration Load(1.3 ms)SELECTadministrations。* FROMadministrationsWHEREadministrations。user_id= $ 1 ANDadministrations。calendar_id= $ 2 LIMIT 1 [[user_id,1],[calendar_id,1] ]
2015-10-06T15:16:00.890698 + 00:00 app [web.1]:在20ms内完成200 OK(查看:10.1ms | ActiveRecord:5.2ms)
2015-10-06T15:16:00.894553 + 00:00 heroku [router]:at = info method = GET path =/ calendarshost = calendy.herokuapp.com request_id = 84199c51- e02b-43a1-8bf8-ac14b102146d fwd =24.205.62.204dyno = web.1 connect = 0ms service = 26ms status = 200 bytes = 2728
2015-10-06T15:16:02.887552 + 00:00 heroku [路由器]:at = info method = GET path =/ calendars / 1host = calendy.herokuapp.com request_id = 3ebd4dd2-ecad-4bef-8bdc-ac3f61a807ac fwd =24.205.62.204dyno = web.1 connect = 1ms服务= 66ms状态= 200字节= 8538
2015-10-06T15:16:02.826123 + 00:00应用[web.1]:2015年10月24日24.205.62.204开始GET/ calendars / 1 06 15:16:02 +0000
2015-10-06T15:16:02.858126 + 00:00 app [web.1]:Post Exists(3.9ms)SELECT 1 AS one FROMpostsWHEREposts 。calendar_id= $ 1 LIMIT 1 [[calendar_id,1]]
2015-10-06T15:16:02.834055 + 00:00 app [web.1]:用户负载(1.7ms)SELECTusers 。* FROMusersWHEREusers。id= $ 1 ORDER BYusers。idASC LIMIT 1 [[id,1]]
2015-10-06T15:16: 02。 883375 + 00:00 app [web.1]:在54ms内完成200 OK(查看:37.0ms | ActiveRecord:8.3ms)
2015-10-06T15:16:02.828856 + 00:00 app [web.1]:通过CalendarsController处理#显示为HTML
2015-10-06T15:16:02.828863+ 00:00 app [web.1]:参数:{id=>1}
2015-10-06T15:16:02.839230 + 00:00 app [web.1]:Calendar Load *FROM日历INNER JOIN管理ON日历。id=管理。calendar_idWHERE管理。user_id= $ 1和日历。 ID= $ 2 LIMIT 1 [[user_id,1],[id,1]]
2015-10-06T15:16:02.831152 + 00:00 app [web.1]:Calendar Load 1.3ms)SELECTcalendars。* FROMcalendarsWHEREcalendars。id= $ 1 LIMIT 1 [[id,1]]
2015-10-06T15:16:02.877428 + 00: 00 app [web.1]:在布局/日历中渲染日历/ show.html.erb(34.0ms)
2015-10-06T15:16:44.490246 + 00:00 app [web.1]:开始POST / calendars / 1 / postsfor 24.205.62.204 at 2015-10-06 15:16:44 +0000
2015-10-06T15:16:44.498537 + 00:00 app [web.1] :(开始
2015-10-06T15:16:4 4.501575 + 00:00 app [web.1]:在8ms内完成500次内部服务器错误(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,日期(2i)=>10,日期(3i)=>6,时间(1i)=2015,时间(2i) (3i)=6,时间(4i)=>15,时间(5i)=>16,主题=> ;Launch,format=>Image,copy=>嘿,Calendy正在启动,看看这里!,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;名称= \ 讯息[图像] \; filename = \calendy-social-media-calendar-tool-collaboration.png\\r\\\
Content-Type:image / png\r\\\
>},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]:日历加载(1.4ms)SELECT日历。* FROM日历WHERE日历。id= $ 1 LIMIT 1 [[id,1]]
2015-10-06T15:16:44.493113 + 00:00 app [web。 1]:通过PostsController处理#以HTML格式创建
2015-10-06T15:16:44.502896 + 00:00应用[web.1]:Paperclip :: Error(post_model缺少attr_accessor for'image_file_name'):
2015-10-06T15:16:44.502898 + 00:00 app [web.1]:app / controllers / posts_controller.rb:30:在`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.ic ohost = calendy.herokuapp.com request_id = 46565200-4c91-4251-9bc3-bf8167c515c8 fwd =24.205.62.204dyno = 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 / postshost = calendy.herokuapp.com request_id = 214a9ce9-deca-4ef1-954a-08b741c712fe fwd =24.205.62.204dyno = web.1 connect = 1ms service = 129ms status = 500 bytes = 1714
更新Heroku CLI ...完成。更新至3.42.15

Paperclip :: Error因为我的 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
  • li> image_updated_at


    或者是迁移问题,在我参考中提到的另外两个Stack Overflo问题中在这个问题的开始?

    解决方案

    请尝试以下操作:

    <$ p: 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])
    结束


    First, let me specify that I did find two questions on Stack Overflow that are very similar to my problem:

    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?

    解决方案

    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
    

    这篇关于Rails 4:回形针错误Post模型缺少attr_accessor for'image_file_name'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆