草庐IT

create_method

全部标签

ruby-on-rails - rails ActiveSupport :Concern and Private Methods

这是关于rails中关注的好主意:http://37signals.com/svn/posts/3372-put-chubby-models-on-a-diet-with-concerns制作不属于公共(public)API的非常小的方法也是一个好主意。如果不使用关注点,这些将成为ruby​​类中的私有(private)方法。在RailsActiveSupport::Concern模块中创建私有(private)方法是否有意义?如果是这样,private是否对关注点定义中的常规实例方法和类方法都有效? 最佳答案 Doesitmake

ruby-on-rails - alias_method_chain 是 alias_method 的同义词吗?

如果这两个方法只是同义词,为什么人们还要费心写额外的字符“_chain”? 最佳答案 没有。alias_method是Ruby的标准方法。alias_method_chain是一个Rails附加组件,旨在简化将旧方法别名为新名称然后将新方法别名为原始名称的常见操作。因此,例如,如果您正在创建具有新功能new_feature的method方法的新版本,则以下两个代码示例是等效的:alias_method:method_without_new_feature,:methodalias_method:method,:method_with

ruby-on-rails - 错误 : Attempt to call private method

来自C风格语法的悠久历史,现在正在尝试学习Ruby(onRails),我一直在分享它的习语等问题,但今天我遇到了一个我没想到的问题有问题,但我看不到必须就在我面前的是什么。我有一个Binary类,它包含一个私有(private)方法,用于从路径值派生URI值(uri和路径是该类的属性)。我从Binary.upload()中调用self.get_uri_from_path(),但我得到:Attempttocallprivatemethod模型的片段如下所示:classBinary我是不是打错电话了?我是否遗漏了其他更基本的东西?目前,唯一调用Binary.get_uri_from_pat

ruby-on-rails - 在 rake db :create 期间无法将 fixnum 转换为字符串

刚刚使用Rails3.0创建了一个新的博客应用我的模型很简单:classPost我使用了命令:railsgeneratescaffoldposttitle:stringbody:textetc.创建这些文件。现在我想使用以下方法生成数据库:rake数据库:创建我得到了错误:rakeaborted!can'tconvertFixnumintoString知道问题是什么吗?我正在学习本教程:http://sixrevisions.com/web-development/how-to-create-a-blog-from-scratch-using-ruby-on-rails/这是痕迹:**

ruby - 什么相当于 Ruby 中的 "method reference"

例如在python中,可以将方法分配给变量:classMyClassdefmyMethod(self):return"Hi"x=MyClass()method=x.myMethodprintmethod()#printsHi我知道这在Ruby中应该是可能的,但我不知道语法是什么。 最佳答案 您需要使用method以方法名称作为参数来获取该方法。这将返回一个Method类型的实例,可以用call()调用它。classMyClassdefmyMethod"Hi"endendx=MyClass.newm=x.method(:myMetho

ruby-on-rails - 为什么 alias_method 在 Rails 模型中失败

classCountry在第一次调用alias_method时失败:NameError:undefinedmethod`langEN'forclass`Country'我的意思是当我执行Country.first时它失败了。但在控制台中,我可以成功调用Country.first.langEN,并且看到第二个调用也有效。我错过了什么? 最佳答案 ActiveRecord使用method_missing(AFAIK通过ActiveModel::AttributeMethods#method_missing)在第一次调用时创建属性访问器和

ruby-on-rails - Rails first_or_create ActiveRecord 方法

first_or_create/first_or_create!方法在Rails中有什么作用?根据documentation,方法“没有描述”... 最佳答案 来自Guides先创建first_or_create方法检查first是否返回nil。如果确实返回nil,则调用create。这在与where方法结合使用时非常强大。让我们看一个例子。假设您想找到一个名为“Andy”的客户,如果没有,请创建一个并将其锁定属性设置为false。你可以通过运行来做到这一点:Client.where(:first_name=>'Andy').fir

ruby-on-rails - 名称错误 : undefined local variable or method `logger'

当我运行“脚本/服务器”时,一切正常,但是当我运行我的单元测试(raketest:units)时,我得到了下面的错误,我不知道如何解决这个问题.错误NameError:undefinedlocalvariableormethod`logger'for#/Users/kamilski81/Sites/pe/vitality_mall/vendor/rails/actionpack/lib/action_controller/test_process.rb:471:in`method_missing'/Users/kamilski81/Sites/pe/vitality_mall/lib/

ruby-on-rails - rails 中的 create_or_update 方法

ifClassName.exists?(["id=?",self.id])object=ClassName.find_by_name(self.name)object.update_attributes!(:street_address=>self.street_address,:city_name=>self.city_name,:name=>self.org_unit_name,:state_prov_id=>self.state_prov_id,:zip_code=>self.zip_code)elseClassName.create!:street_address=>self.

ruby - RSpec NoMethodError : "undefined method ` describe' for main Object"

我正在尝试学习Rspec。我在eclipse中的ruby项目如下-代码-require'rspec'require'./RubyOffRailsTuts/classes/furlong'describeFurlongdoend错误-/RubyOffRailsTuts/specs/furlong_spec.rb:6:in`':undefinedmethod`describe'formain:Object(NoMethodError)没有在网上得到任何有用的答案。我该如何解决这个问题? 最佳答案 作为RSpec.describe的前缀d