Tin*_*n81 5 ruby-on-rails rails-activestorage
在我的 Rails 5.2.2 应用程序中,我使用 Active Storage 附加logos到profiles:
class Profile < ApplicationRecord
has_many_attached :logos
end
Run Code Online (Sandbox Code Playgroud)
如何测试更新时个人资料中是否附加了新徽标?
现在,我正在检查logos控制器中是否有一个工作正常的参数:
class ProfileController < ApplicationController
def update
if params[:profile][:logos].present?
@profile.logo_active = true # This is the important attribute I need to set
end
if @profile.update(profile_params)
flash[:success] = "Profile updated."
redirect_to profile_path
end
end
end
Run Code Online (Sandbox Code Playgroud)
有没有办法在模型中执行相同的操作,即检查附件是否实际上是新的,然后logo_active根据该附件设置属性?
我尝试了 Rails 的new_record?方法,但没有成功。
您可以检查是否有附件
@profile.logos.attached?
Run Code Online (Sandbox Code Playgroud)
https://api.rubyonrails.org/classes/ActiveStorage/Attached/Many.html#method-i-attached-3F
| 归档时间: |
|
| 查看次数: |
5164 次 |
| 最近记录: |