未定义的方法 `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 """

提取的源代码(围绕第 8 行):

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 (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...