我正在编写Ruby on Rails 3教程书并在命令行上输入以下内容:
rake db:migrate
Run Code Online (Sandbox Code Playgroud)
产生了以下警告.
WARNING: Global access to Rake DSL methods is deprecated. Please Include
... Rake::DSL into classes and modules which use the Rake DSL methods.
WARNING: DSL method DemoApp::Application#task called at /Users/imac/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/railties-3.0.7/lib/rails/application.rb:215:in `initialize_tasks'
Run Code Online (Sandbox Code Playgroud)
我不知道该怎么做或如何使用它.我不知道Rake的任何其他命令.
我该如何解决这个问题?
是否有更正确的方法以逗号分隔的字符串输出数组的内容
@emails = ["joe@example.com", "Peter@example.com", "alice@example.com"]
@emails * ","
=> "joe@example.com", "Peter@example.com", "alice@example.com"
Run Code Online (Sandbox Code Playgroud)
这有效,但我相信必须有一个更优雅的解决方案.
我想仅在输入电子邮件时验证电子邮件.
我尝试了以下方法:
validates :email, :presence => {:message => "Your email is used to save your greeting."},
:email => true,
:if => Proc.new {|c| not c.email.blank?},
:uniqueness => { :case_sensitive => false }
Run Code Online (Sandbox Code Playgroud)
但是这不起作用,因为它会在电子邮件留空时阻止显示错误消息.
如何在缺少电子邮件时验证电子邮件是否存在,并且仅在输入时验证格式?
我想删除我在ubuntu上创建的用户.
但是当我使用以下命令时:
userdel -r cafe_fixer
Run Code Online (Sandbox Code Playgroud)
我收到以下消息:
user cafe_fixer is currently used by process 15945
Run Code Online (Sandbox Code Playgroud)
我没有将此用户用于我刚创建的任何内容,现在希望将其删除.
任何帮助赞赏.
如果有孩子,我试图阻止记录被销毁.
class Submission < ActiveRecord::Base
has_many :quotations, :dependent => :destroy
before_destroy :check_for_payments
def quoted?
quotations.any?
end
def has_payments?
true if quotations.detect {|q| q.payment}
end
private
def check_for_payments
if quoted? && has_payments?
errors[:base] << "cannot delete submission that has already been paid"
false
end
end
end
class Quotation < ActiveRecord::Base
#associations
belongs_to :submission
has_one :payment_notification
has_one :payment
before_destroy :check_for_payments
private
def check_for_payments
if payment_notification || payment
errors[:base] << "cannot delete quotation while payment exist"
return false
end
end
end
Run Code Online (Sandbox Code Playgroud)
当我测试此代码时,before_destroy:check_for_payments会阻止删除Quotation记录.
但是:提交before_destroy回调中的:check_for_payments不会停止提交被删除. …
我的控制器中有以下内容:
redirect_to signin_path, :notice => "The email is already registered"
Run Code Online (Sandbox Code Playgroud)
在我看来,我有
<%= flash[:notice] if flash[:notice] %>
Run Code Online (Sandbox Code Playgroud)
但是没有出现flash消息.
但是,如果我在控制器中执行以下操作
flash[:notice] = "There is already an acount for this email. Please Login to create your board."
redirect_to signin_path
Run Code Online (Sandbox Code Playgroud)
它确实有效.第一个不起作用的原因是什么?
我想在我的本地计算机上上传图像以进行开发,但是将它们存储在我的Amazon S3帐户上以进行生产.
upload.rb
if Rails.env.development?
has_attached_file :photo, :styles => { :thumb => '40x40#', :medium => '150x200>', :large => '300x300>'},
:convert_options => { :thumb => "-quality 92", :medium => "-quality 92", :large => "-quality 92" },
:processors => [:cropper]
else
has_attached_file :photo, :styles => { :thumb => '40x40#', :medium => '150x200>', :large => '300x300>'},
:convert_options => { :thumb => "-quality 92", :medium => "-quality 92", :large => "-quality 92" },
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":attachment/:id/:style.:extension",
:bucket => …Run Code Online (Sandbox Code Playgroud) 对于红宝石,我可以做一个计数器,或者我必须做以下事情
count = 0
4.times do
puts "this is the count #{count}"
count = count+1
Run Code Online (Sandbox Code Playgroud) 我有一个在创建对象后调用的方法
after_create :send_welcome_email
Run Code Online (Sandbox Code Playgroud)
有没有办法将此限制为条件,例如对象属性的值
after_create :send_welcome_email unless self.role == "Celebrant"
Run Code Online (Sandbox Code Playgroud)
例如?
这是一个真正的超新手问题.
我有一个存储在数据库中的年龄.
当我从数据库中获得年龄时,我想获得每个数字.
例:
User.age = 25
Run Code Online (Sandbox Code Playgroud)
我想得到以下内容:
first = 5
second = 2
Run Code Online (Sandbox Code Playgroud)
我似乎无法从数据中将其作为修复数量来解决这个问题.
任何人都知道干净简单的解决方案.