【问题标题】:Active storage problem - replacing pictures主动存储问题——替换图片
【发布时间】:2022-11-13 15:49:21
【问题描述】:

我在用着:
ruby 2.6
rails 6.1.4
devise
devise_token_auth
active-storage 使用本地磁盘服务

我正在创建可以存储和上传图片的 api。我可以通过 Insomnia 上传图片,但问题是当我想向同一个用户添加第二张图片时。图像只是替换。

user.rb

# frozen_string_literal: true

class User < ActiveRecord::Base  
  
  def initialize
    self.pictures = []
  end

  extend Devise::Models #added this line to extend devise model
  # Include default devise modules. Others available are:
  # :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
        :recoverable, :rememberable, :validatable, :confirmable
  include DeviseTokenAuth::Concerns::User

  VALID_USERNAME_REGEX= /^(?![_.])(?!.*[_.]{2})[a-zA-Z0-9._]+(?<![_.])$/ix
  validates :username, presence: true, length: {minimum:3, maximum:26},
            format: { with: VALID_USERNAME_REGEX, :multiline => true,
            message: :invalid_username }
  validate :password_complexity

  attr_accessor :pictures

  has_many_attached :pictures
  validates :pictures, content_type: ['image/png', 'image/jpg', 'image/jpeg'],
            size: { less_than: 5.megabytes , message: :invalid_size }

  def password_complexity
    return if password.blank? || password =~ /^((?!.*[\s]))/
    errors.add :password, :invalid_password
  end
end

application_controller.rb

class ApplicationController < ActionController::API
  include DeviseTokenAuth::Concerns::SetUserByToken

  before_action :configure_permitted_parameters, if: :devise_controller?

  protected
    def configure_permitted_parameters        
      devise_parameter_sanitizer.permit(:account_update, keys: [:pictures, :username, :email])
    end
end

我使用的所有控制器都默认来自devise_token_auth。我找到了要添加的提示config/application.rb

 config.active_storage.replace_on_assign_to_many = false

但在那之后我总是得到status 500

【问题讨论】:

    标签: ruby-on-rails ruby api rails-activestorage


    【解决方案1】:

    您是否在参数中添加了一个数组以便可以存储多个图像?

    def configure_permitted_parameters        
          devise_parameter_sanitizer.permit(:account_update, keys: [:pictures [], :username, :email])
        end
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    • 我更新了主题的代码(user.rb 并添加了应用程序控制器)。不幸的是,现在我遇到了错误:“exception”:“#<NoMethodError: undefined method `attached?'对于 #<String:0x0000555f02239298>>"
    • 在您的应用程序控制器中,您必须将一个数组添加到您的参数中,以便您可以存储多个图像。我正在更新答案,以便您看到。
    • @lucas_grandviergne #&lt;ArgumentError: wrong number of arguments (given 0, expected 1..2)&gt;。在出现该错误后的 Insomnia 中,我将 json "pictures": 更改为 "pictures []",但它不起作用。我怎么能给出这些论点?
    【解决方案2】:

    我通过将其添加到 config/environment/development.rb 解决了类似的问题(根据 Rails 指南):

    config.active_storage.replace_on_assign_to_many = false
    

    祝你好运!

    【讨论】:

      【解决方案3】:

      解决这个问题的最好方法是打开 application.rb 并在你的模块中添加。

      config.active_storage.replace_on_assign_to_many = false
      

      这就是你需要做的。它有效,只需确保保存所有文件,然后重新启动服务器。

      您可以在 rails docs 中看到它说以下内容:

      分配给附件集合时,可选择替换现有文件而不是添加到它们(如@user.update!(图像:[…]))。使用 config.active_storage.replace_on_assign_to_many 来控制这种行为。

      Rails Guides

      【讨论】:

        猜你喜欢
        • 2014-11-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多