草庐IT

argument-validation

全部标签

ruby-on-rails - rails : validation fails with before_create method on datetime attribute

ApiKey.create!抛出验证错误:expires_at不能为空。classApiKey有属性t.datetime:expires_at但是,如果删除了验证,则before_create方法会在创建时起作用。这是为什么?-此模式适用于其他属性,例如access_tokens(字符串)等 最佳答案 我会说因为before_create在验证之后运行,也许您想将before_create替换为before_validation注意:如果你像那样留下回调,它会在你运行valid?或save或任何时候设置到期日期触发验证的事件记录方法

ruby-on-rails - 类型错误 : wrong argument type Class (expected Module) with Controller concern

所以我有一个名为guests_controller.rb的Controller,如下所示:classGuestsController而且我在controllers/concens/中还有一个名为data.rb的问题:moduleDataextendActiveSupport:Concerndefsave_data(save_method)casesave_methodwhen"db"@guest=Guest.new(guest_params)if@guest.saveflash[:success]="Newguestentrycreated!"redirect_toguests_pat

sql - Rails ActiveRecord Arels : Unsupported argument type: String. 改为构造一个 Arel 节点

我正在使用Arels创建查询。在此查询中,我使用了generate_series函数。这是我的代码:defgenerate_seriesArel::Nodes::NamedFunction.new('GENERATE_SERIES',[start_date,end_day,'1day'])enddefstart_dateArel::Nodes::SqlLiteral.new(当我尝试通过generate_series.to_sql进行测试时。我遇到异常:Arel::Visitors::UnsupportedVisitError:Unsupportedargumenttype:Strin

ruby-on-rails - 我如何解决 "bad argument (expected URI object or URI string)"? Ruby on Rails,Michael Hartl 教程

我正在关注RubyonRailsTutorial作者:MichaelHartl。我到达了Chapter11.37但我的测试失败了。我收到以下错误:Failure/Error:xhr:post,:create,relationship:{followed_id:other_user.id}ArgumentError:badargument(expectedURIobjectorURIstring)我是RubyonRails的新手,所以我真的不知道出了什么问题。有人可以帮助解决此错误吗?controllers/relationships_controller.rb:classRelatio

ruby-on-rails - rails - rspec - 为什么这个 "validates_inclusion_of :role, :in => %w[one, two, three ] }"不起作用

我收到一个错误消息,指出此方法不存在,但这看起来相当基本,我不确定为什么会收到此错误消息?it{shouldvalidate_presence_of:role}#ThisisOK.Failure/Error:it{shouldvalidate_inclusion_of:role,:in=>%w[onetwothree]}NoMethodError:undefinedmethod`validate_inclusion_of'for#对于代码:classUser[one,twothree]]http://apidock.com/rails/ActiveModel/Validations/C

ruby - gem 安装失败,显示 "ruby: invalid option -H (-h will show valid options) (RuntimeError)"

在OSXMountainlion上运行geminstallffi会产生以下错误。geminstallffiBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingffi:ERROR:Failedtobuildgemnativeextension./beweiche.rvm/rubies/ruby-1.9.3-p374/bin/rubyextconf.rb/beweiche.rvm/rubies/ruby-1.9.3-p374/bin/ruby:invalidoption-H(-hwillshowvalido

ruby - 是否可以结合 with_options 和 :if for conditional validations?

我正在尝试使用with_options为管理员用户将我的条件验证组合在一起。对用户名唯一性的第二次验证最终覆盖了with_options条件。有更好的方法吗?或者我应该忘记with_options并编写两个单独的语句?with_options:if=>Proc.new{|user|user.admin?}do|admin|admin.validates:email,:presence=>trueadmin.validates:username,:uniqueness=>true,:if=>Proc.new{|user|user.category=="customized_usernam

ruby-on-rails - FactoryGirl 问题 - `factory' : wrong number of arguments (0 for 1) (ArgumentError)

我正在学习RubyonRails教程,我已经接近尾声了,但是FactoryGirl并没有让我轻松完成。我无法运行任何需要工厂的规范文件。require'spec_helper'require'factory_girl_rails'describeUserdobefore(:each)do@attr={:name=>"ExampleUser",:email=>"user@example.com",:password=>"foobar",:password_confirmation=>"foobar"}endit"shouldcreateanewinstancegivenvalidattr

ruby-on-rails - Heroku|Ruby 参数错误 : Missing required arguments:

我在/config/initializers/carrier_wave.rb中添加了aws访问信息:ifRails.env.production?CarrierWave.configuredo|config|config.fog_credentials={#ConfigurationforAmazonS3:provider=>'AWS',:aws_access_key_id=>ENV['access_key'],:aws_secret_access_key=>ENV['secret_access_key'],}config.fog_directory=ENV['bucket']ende

ruby - 液体错误 : wrong number of arguments

我正在尝试一个简单的Jekyll插件:classMonthlyArchives当我尝试按如下方式在页面中运行它时:{%monthly_archives1%}我收到Liquid错误:参数数量错误(2代表0)。有什么想法吗? 最佳答案 我还没有机会用Liquid构建一些东西,但是Jekyllwikipageaboutbuildingyourownplugins在注册之前让整个类(class)都被模块包围moduleJekyll...yourcode...endLiquid::Template.register_tag('monthly_