【问题标题】:ActiveSupport::MessageVerifier::InvalidSignature in RegistrationsController#createActiveSupport::MessageVerifier::InvalidSignature 在 RegistrationsController#create
【发布时间】:2019-07-18 23:59:35
【问题描述】:

我目前正在开发一个安装了ActiveStorage 的Rails 6 应用程序。我正在使用设计进行身份验证。尝试在注册表单上创建新用户时出现以下错误。

ActiveSupport::MessageVerifier::InvalidSignature in RegistrationsController#create

我认为原因来自于尝试为模型User 设置默认头像。创建用户时,我尝试将astronaut.svg 设置为默认头像。

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
  has_many :posts
  has_one_attached :avatar

  before_create :set_defaults

  def set_defaults
    self.avatar = 'assets/images/astronaut.svg' if self.new_record?
  end
end

如何解决?

【问题讨论】:

    标签: ruby devise rails-activestorage ruby-on-rails-6


    【解决方案1】:

    此代码适用于我:

    def set_defaults
      if self.new_record?
        self.avatar = Rack::Test::UploadedFile.new(
          Rails.root.join('app/assets/images/astronaut.png'),
          'image/png',
        )
    
        # file = File.new(Rails.root.join('app/assets/images/astronaut.png'))
        # self.avatar = Rack::Test::UploadedFile.new(
        #   file.path,
        #   Mime::Type.lookup_by_extension(File.extname(file).strip.downcase[1..-1]).to_s,
        # )
      end
    end
    

    但是我建议不要在before_create 中发送默认图像,而是使用帮助器:

    def user_avatar(user)
      if user.avatar.attached?
        image_tag user.avatar
      else
        image_tag 'astronaut.png'
      end
    end
    

    【讨论】:

    • 所以使用:after_create user_avatar(user)
    • 您可以在视图中使用user_avatar
    • 哦,我明白了,所以不要在模型中使用它。
    猜你喜欢
    • 1970-01-01
    • 2018-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-24
    • 2018-11-02
    • 1970-01-01
    相关资源
    最近更新 更多