Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在Action text Rails 6上验证富格文本中的附件?_Ruby On Rails_Actiontext - Fatal编程技术网

Ruby on rails 如何在Action text Rails 6上验证富格文本中的附件?

Ruby on rails 如何在Action text Rails 6上验证富格文本中的附件?,ruby-on-rails,actiontext,Ruby On Rails,Actiontext,如果content.embeds为空,即使在附加图像时,我如何检查我的附件?但当我在视图中使用content.embedded时,它会显示我的图像。对不起我的英语。谢谢。回答下面的问题 rich_text_content.body.attachments 创建模型时返回附件,我可以将其用于有效的 class Post < ApplicationRecord has_rich_text :content validates :content, presence: true

如果content.embeds为空,即使在附加图像时,我如何检查我的附件?但当我在视图中使用content.embedded时,它会显示我的图像。对不起我的英语。谢谢。

回答下面的问题 rich_text_content.body.attachments 创建模型时返回附件,我可以将其用于有效的

class Post < ApplicationRecord
    has_rich_text :content

    validates :content, presence: true
    validate :content_length
    validate :content_embeds

    def content_embeds
       if content.embeds.any?
           errors.add(:content, 'Нельзя загружать более 4 изображений') if content.embeds.size > 4
           content.embeds.each do |attach|
               errors.add(:content, 'Изображение не может весить > 10 мБ') if attach.byte_size > 1242880
               errors.add(:content, 'Можно загружать только изображения') unless attach.image? 
               errors.add(:content, 'Недопустимый формат изображения') unless attach.content_type == 'image/jpeg' || attach.content_type == 'image/png'
            end
        end
    end 

    def content_length 
        max_length = 50
        symbol_size = 0

        if content.embeds.any? 
           content.embeds.each do |attach|
              symbol_size += (attach.filename.size + 2)
           end
        end

        errors.add(:content, 'Слишком длинный текст') if (content.to_plain_text.size - symbol_size) > max_length  
        content.embeds.each { |attach| puts "ATTACH #{attach}"}
    end