我认为我的用户工厂正在构建中存在问题.我收到一个错误,说密码不能为空,但它明确地设置在我的workers.rb中.有没有人看到我可能遗失的任何东西?或者为什么规范失败的原因?我为其他一个模型做了一个非常类似的事情,它似乎很成功.我不确定错误是否是由设计引起的.
User should create a new instance of a user given valid attributes
Failure/Error: User.create!(@user.attributes)
ActiveRecord::RecordInvalid:
Validation failed: Password can't be blank
# ./spec/models/user_spec.rb:28:in `block (2 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)
Factory.define :user do |user|
user.name "Test User"
user.email "user@example.com"
user.password "password"
user.password_confirmation "password"
end
Run Code Online (Sandbox Code Playgroud)
require 'spec_helper'
describe User do
before(:each) do
@user = Factory.build(:user)
end
it "should create a new instance of a user given valid attributes" do
User.create!(@user.attributes)
end
end
Run Code Online (Sandbox Code Playgroud)
class User < ActiveRecord::Base
# …
Run Code Online (Sandbox Code Playgroud) 是否可以在新的app/assets/application.js文件中注释掉这一行?如果是这样,怎么样?
//=require jquery_ujs
Run Code Online (Sandbox Code Playgroud)
我的意思是,它已被注释掉,以避免被误解为CoffeeScript或JavaScript,但它显然仍然是一个目的.
我的应用程序中有两个下拉框.根据第一个组合框中选择的值,应填充第二个下拉框中的值.这些值应来自数据库.
请帮我.
阅读gmaps4rails gem文档,我没有找到任何设置地图宽度和高度的方法.有没有办法做到这一点?
(我复制/粘贴我在Codereview上发布的相同问题:https://codereview.stackexchange.com/questions/1747/good-practice-loop-and-if-statement )
我想知道最佳做法是什么:
版本A:
loop1
if condition1
code1
if condition2
code2
if condition3
code3
Run Code Online (Sandbox Code Playgroud)
或者,版本B:
if condition1
loop1 with code1
if condition2
loop1 with code2
if condition3
loop1 with code3
Run Code Online (Sandbox Code Playgroud)
我已经实现了版本B,因为它对我来说更具可读性,并且始终检查相同的条件似乎很荒谬.
但循环n次相同的阵列也可能被视为荒谬:)
我正在尝试使用money gem在我的应用程序中处理货币,但我遇到了一个奇怪的错误.这就是我在"记录"模型中的含义:
composed_of :amount,
:class_name => "Money",
:mapping => [%w(cents cents), %w(currency currency_as_string)],
:constructor => Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) },
:converter => Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't convert #{value.class} to Money") }
Run Code Online (Sandbox Code Playgroud)
amount是一个整数.
当我创建一个新记录时,它会忽略我在amount字段中输入的任何值,并将其默认为0.我需要在表单中添加一些内容吗?
我使用的是rails 3.0.3,而money gem版本是3.5.5
当我运行涉及启用了gmaps4rails的模型的rake任务时,我得到了这个错误,如果我对模型进行评论,那么它就不是acts_as_gmappable,而是正确完成.
enter code here
troy$ rake populate:scans --trace
** Invoke populate:scans (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute populate:scans
http://goo.gl/fb/977ze
Sat, 16 Jul 2011 19:43:59 GMT
47.676506
-122.121872
91df0f32209c5212XXXXXXXXXXXXXXX
rake aborted!
undefined method `gmaps' for #<Scan:0x000001051fb9a8>
/Users/troy/.rvm/gems/ruby-1.9.2-p180/gems/activemodel-3.0.9/lib/active_model/attribute_methods.rb:392:in `method_missing'
/Users/troy/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/attribute_methods.rb:46:in `method_missing'
/Users/troy/.rvm/gems/ruby-1.9.2-p180/gems/gmaps4rails-0.9.1/lib/gmaps4rails/acts_as_gmappable.rb:12:in `process_geocoding'
/Users/troy/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.9/lib/active_support/callbacks.rb:415:in `_run_validate_callbacks'
/Users/troy/.rvm/gems/ruby-1.9.2-p180/gems/activemodel-3.0.9/lib/active_model/validations.rb:212:in `run_validations!'
/Users/troy/.rvm/gems/ruby-1.9.2-p180/gems/activemodel-3.0.9/lib/active_model/validations/callbacks.rb:67:in `block in run_validations!'
/Users/troy/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.9/lib/active_support/callbacks.rb:414:in `_run_validation_callbacks'
/Users/troy/.rvm/gems/ruby-1.9.2-p180/gems/activemodel-3.0.9/lib/active_model/validations/callbacks.rb:67:in `run_validations!'
/Users/troy/.rvm/gems/ruby-1.9.2-p180/gems/activemodel-3.0.9/lib/active_model/validations.rb:179:in `valid?'
/Users/troy/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/validations.rb:55:in `valid?'
/Users/troy/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/validations.rb:75:in `perform_validations'
/Users/troy/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/validations.rb:43:in `save'
/Users/troy/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/attribute_methods/dirty.rb:21:in `save'
/Users/troy/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/transactions.rb:240:in `block (2 levels) in save'
/Users/troy/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/transactions.rb:292:in `block in with_transaction_returning_status'
/Users/troy/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
/Users/troy/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/transactions.rb:207:in …
Run Code Online (Sandbox Code Playgroud) 我的设置:Rails 3.0.9,Ruby 1.9.2
我有理由这样做,但我需要的是一种动态添加虚拟属性到activerecord结果集的方法.这意味着我没有attr_accessor
在模型中使用,而是希望动态地将虚拟属性添加到结果集中.
例如,
users = User.all
#a user has following attributes: name, email, password
Run Code Online (Sandbox Code Playgroud)
我喜欢做的是说增加(不使用attr_accessor
)虚拟属性status
来users
,这可能吗?
ruby ×8
gmaps4rails ×2
google-maps ×2
rspec ×2
activerecord ×1
c# ×1
currency ×1
devise ×1
factory-bot ×1
guard ×1
java ×1
jruby ×1
macruby ×1
php ×1
qt ×1
rubygems ×1
spork ×1
sprockets ×1