如何解决未定义的方法 `to_f' 用于...日期、Ruby on Rails、Paperclip - 未捕获 image_url,或因此呈现图像
我一直在尝试向网站添加博客功能,并使用现有代码库作为参考。我在为每篇文章添加和渲染图像时遇到了很多麻烦,使用已经设置的 Paperclip/AWS 系统并使用其他资源。
我最近更新了架构,因为“article_image_file_size”类型是一个字符串,所以我进行了迁移以成功将类型更改为整数。
image of database article columns and values
现在我的文章文件大小出现在数据库中,但我仍然没有捕获“article_image_url”,以至于它没有显示在数据库中,我也无法在文章页面。
我的目标是让 article_image_url 被回形针捕获,从而使其能够在浏览器模板 (article/show.rb) 中呈现图像
我收到错误:
""" app/views/articles/show.html.erb 其中第 8 行提出:
2021 年 6 月 29 日星期二的未定义方法 `to_f':日期 你的意思? to_s """
show.html.erb
我的文章模型应该捕获图像,使用函数 set_article_image(在底部)、has_attached_file、validates_attachment 和 before_validation:
attr_accessor :article_image_url
belongs_to :user
validates :title,presence: true,length: { minimum: 3,maximum: 50 }
validates :body,length: { minimum: 10,maximum: 1000 }
validates :user_id,presence: true
has_many :comments
accepts_nested_attributes_for :comments,allow_destroy: true
has_attached_file :article_image,# path: ":rails_root/public/system/:class/:id/:attachment/:basename_:style.:extension",# url: "/system/:class/:id/:attachment/:basename_:style.:extension",styles: {
thumb: ['100x100#',:jpg,:quality => 70],preview: ['480x480#',medium: ['600>',},default_url: "default_article_:style.jpg",convert_options: {
thumb: '-set colorspace sRGB -strip',preview: '-set colorspace sRGB -strip',medium: '-set colorspace sRGB -strip',}
validates_attachment :article_image,size: { :in => 0..10.megabytes },content_type: { :content_type => /^image\/(jpeg|jpg|png|gif|tiff)$/ }
before_validation :set_article_image
def set_article_image
self.article_image = URI.parse(self.article_image_url) if self.article_image_url.present?
# super
end
end
这是文章模型所基于的其他特征(书籍)模型的模型,其中书籍封面图像及其 URL 被捕获并在浏览器中呈现:
...
attr_accessor :cover_url
self.per_page = 18
has_many :reviewers,through: :book_reviews,source: :user
has_many :book_reviews,dependent: :destroy
has_many :public_book_reviews,-> { where(dropped_book: false) },class_name: BookReview
has_many :user_books,dependent: :destroy
has_many :users,through: :user_books
has_many :book_requests,dependent: :destroy
...
accepts_nested_attributes_for :categorizations,allow_destroy: true
accepts_nested_attributes_for :collections_books,allow_destroy: true
# Scopes
scope :visible,-> { where(hidden: false) }
scope :hidden,-> { where(hidden: true) }
...
validates :author,:ISBN,presence: true
validates :title,uniqueness: { scope: :author,message: 'is already present for this author' }
validates :lexile,numericality: { greater_than_or_equal_to: 0,less_than_or_equal_to: 2500 }
has_attached_file :cover,styles: {
thumb: ['100x100#',default_url: "default_book_:style.jpg",convert_options: {
thumb: '-set colorspace sRGB -strip',}
validates_attachment :cover,content_type: { :content_type => /^image\/(jpeg|png|gif|tiff)$/ }
before_validation :image_remote_url
...
def image_remote_url
self.cover = URI.parse(self.cover_url) if self.cover_url.present?
# super
end
...
任何建议将不胜感激。如果需要,我还可以分享更多相关的代码片段。谢谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。