【问题标题】:Styling and Validations in ActiveStorage Rails like PaperclipActiveStorage Rails 中的样式和验证,如 Paperclip
【发布时间】:2019-03-15 02:26:03
【问题描述】:

在我的新项目中,我第一次使用 Active Storage,在此之前我使用的是 Paperclip。在回形针中,我们可以选择为内容类型添加默认样式和验证。喜欢:

has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\z/

我似乎找不到任何关于如何在 ActiveStorage 中实现这些功能的文档。

【问题讨论】:

标签: ruby-on-rails ruby ruby-on-rails-5 paperclip rails-activestorage


【解决方案1】:

ActiveStorage 中还没有验证,所以你可以像这样使用custom validator

class MyModel < ApplicationRecord
  has_one_attached :some_img
  validates :some_img, image: true
end


class ImageValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    record.errors[attribute] << I18n.t('errors.file_size') unless file_size_valid?(value)
    record.errors[attribute] << I18n.t('errors.file_type') unless file_type_valid?(value)
  end

  private

  def file_size_valid?(value)
    value.blob.byte_size <= 5.megabytes
  end

  def file_type_valid?(value)
    !value.blob.content_type.starts_with?('image/')
  end
end

没有类似回形针的样式语法,但您可以为此目的使用variant

my_object.some_img.variant(resize: '100x100>')

它将options 直接传递给ImageMagick。

也没有默认图像选项,因此在模型中创建某种方法或作为装饰器可能会有所帮助。

【讨论】:

    【解决方案2】:

    在此链接中https://devcenter.heroku.com/articles/paperclip-s3

    将此添加到 gemfile

    gem 'paperclip'
    gem 'aws-sdk', '~> 2.3'
    

    然后捆绑安装

    config/environments/production.rb

    config.paperclip_defaults = {
      storage: :s3,
      s3_credentials: {
        bucket: ENV.fetch('S3_BUCKET_NAME'),
        access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
        secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
        s3_region: ENV.fetch('AWS_REGION'),
      }
    }
    

    此外,我们需要在 Heroku 应用程序上设置 AWS 配置变量。

    heroku config:set S3_BUCKET_NAME=your_bucket_name
    heroku config:set AWS_ACCESS_KEY_ID=your_access_key_id
    heroku config:set AWS_SECRET_ACCESS_KEY=your_secret_access_key
    heroku config:set AWS_REGION=your_aws_region
    
    $ rails g migration AddAvatarToProfiles
    
    class AddAvatarToProfiless < ActiveRecord::Migration
      def self.up
        add_attachment :profiles, :avatar
      end
    
      def self.down
        remove_attachment :profiles, :avatar
      end
    end
    

    然后rake db:migrate

    这是在视图中

    <%= form_for(@profile, multipart: true) do |f| %>
      <div class="field">
        <%= f.label :name %>
        <%= f.text_field :name %>
      </div>
      <div class="field">
        <%= f.label :avatar %>
        <%= f.file_field :avatar %>
      </div>
      <div class="actions">
        <%= f.submit 'Make a friend' %>
        <%= link_to 'Nevermind', friends_path, class: 'button' %>
      </div>
    <% end %>
    

    这是在控制器内部

    类 ProfilesController

     def create
        @profile = Profile.new(profile_params)
    
        if @profile.save
          redirect_to @friend, notice: 'Profile was successfully created.'
         else
           render action: 'new'
        end
      end
    
      private
    
      def profile_params
        params.require(:profile).permit(:avatar, :name)
      end
    end
    

    【讨论】:

    • 您刚刚为我提供了将回形针与我的应用程序集成的指南。我不需要回形针。我想将 ActiveStorage 集成到我的应用程序中,并具有与 Paperclip 中相同的功能。
    • 是的,通过添加类似这样的东西 rails g migration AddAvatarToProfiles
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2019-06-17
    • 1970-01-01
    • 2018-11-12
    • 2011-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多