Baz*_*ley 5 ruby ruby-on-rails rails-console
在我的Character
模型中,我添加了:
character.rb
before_save do
self.profile_picture_url = asset_path('icon.png')
end
Run Code Online (Sandbox Code Playgroud)
但是,对于数据库中已存在的所有字符,它们profile_picture_url
是nil
.因此,我想进入控制台并遍历所有这些并设置它.在控制台我试过:
Character.find_each do |c|
c.profile_picture_url = asset_path('icon.png')
end
Run Code Online (Sandbox Code Playgroud)
但是这给出了错误:
NoMethodError: undefined method `asset_path' for main:Object
Run Code Online (Sandbox Code Playgroud)
我希望我已经充分地传达了我想要实现的目标.我哪里错了?
查看AssetHelper的文档:
此模块提供了生成资产路径和URL的方法.
您可以通过asset_path
以下几种方式访问控制台:
ActionController::Base.helpers.asset_path('icon.png')
Run Code Online (Sandbox Code Playgroud)
要么
include ActionView::Helpers::AssetUrlHelper
asset_path('icon.png')
Run Code Online (Sandbox Code Playgroud)
另外在旁注和问题分开的情况下,如果你要更新所有的,Character
我会使用update_all作为你的profile_picture_url
属性.
Character.update_all(profile_picture_url: asset_path('icon.png')
Run Code Online (Sandbox Code Playgroud)