我正在回答我自己的问题 - 只是把它放在谷歌这里,以防它帮助其他人.此代码允许您验证列表中是否存在一个字段.请参阅代码中的注释以了解用法 只需将其粘贴到lib/custom_validations.rb中,然后将"custom_validations"添加到environment.rb即可
#good post on how to do stuff like this http://www.marklunds.com/articles/one/312
module ActiveRecord
module Validations
module ClassMethods
# Use to check for this, that or those was entered... example:
# :validates_presence_of_at_least_one_field :last_name, :company_name - would require either last_name or company_name to be filled in
# also works with arrays
# :validates_presence_of_at_least_one_field :email, [:name, :address, :city, :state] - would require email or a mailing type address
def validates_presence_of_at_least_one_field(*attr_names)
msg = attr_names.collect {|a| a.is_a?(Array) ? " ( #{a.join(", ")} …Run Code Online (Sandbox Code Playgroud) 历史:
我很难生成一个可行的网址.我一直在尝试按照此处描述的指示:http://docs.amazonwebservices.com/AmazonCloudFront/latest/DeveloperGuide/index.html?PrivateContent.html
这是我到目前为止所做的...虽然不起作用 - 仍然被拒绝访问:
def url_safe(s)
s.gsub('+','-').gsub('=','_').gsub('/','~').gsub(/\n/,'').gsub(' ','')
end
def policy_for_resource(resource, expires = Time.now + 1.hour)
%({"Statement":[{"Resource":"#{resource}","Condition":{"DateLessThan":{"AWS:EpochTime":#{expires.to_i}}}}]})
end
def signature_for_resource(resource, key_id, private_key_file_name, expires = Time.now + 1.hour)
policy = url_safe(policy_for_resource(resource, expires))
key = OpenSSL::PKey::RSA.new(File.readlines(private_key_file_name).join(""))
url_safe(Base64.encode64(key.sign(OpenSSL::Digest::SHA1.new, (policy))))
end
def expiring_url_for_private_resource(resource, key_id, private_key_file_name, expires = Time.now + 1.hour)
sig = signature_for_resource(resource, key_id, private_key_file_name, expires)
"#{resource}?Expires=#{expires.to_i}&Signature=#{sig}&Key-Pair-Id=#{key_id}"
end
resource = "http://d27ss180g8tp83.cloudfront.net/iwantu.jpeg"
key_id = "APKAIS6OBYQ253QOURZA"
pk_file = "doc/pk-APKAIS6OBYQ253QOURZA.pem"
puts expiring_url_for_private_resource(resource, key_id, pk_file)
Run Code Online (Sandbox Code Playgroud)
谁能告诉我这里我做错了什么?