如果image_tag无法从网址加载图片,我想渲染默认图片:
因此,如果image_tag无法从网址加载图片:
<%= image_tag 'https://something.com/image.jpg' %>
Run Code Online (Sandbox Code Playgroud)
然后呈现默认值:
<%= image_tag image_url("default.png")
Run Code Online (Sandbox Code Playgroud) ApiKey.create!
Run Code Online (Sandbox Code Playgroud)
引发验证错误:expires_at不能为空.
class ApiKey < ActiveRecord::Base
before_create :set_expires_at
validates :expires_at, presence: true
private
def set_expires_at
self.expires_at = Time.now.utc + 10.days
end
end
Run Code Online (Sandbox Code Playgroud)
有属性
t.datetime :expires_at
Run Code Online (Sandbox Code Playgroud)
但是,如果删除了验证,则before_create方法适用于create.
为什么是这样? - 此模式适用于其他属性,例如access_tokens(string)等.
我怎么能只生成含有n个字符的伪随机字符串A-Z,0-9像SecureRandom.base64没有"+","/"和"="?例如:
(0..n).map {(('1'..'9').to_a + ('A'..'Z').to_a)[rand(36)]}.join
Run Code Online (Sandbox Code Playgroud)