草庐IT

my_custom_method

全部标签

ruby-on-rails - rails : Add Custom action to resource

我有一个故事Controller,我已将其映射为资源。我向stories_controller添加了2个新方法,'top'和'latest'。但是当我尝试访问example.com/stories/top时,出现“没有ID=top的故事”错误。如何更改路由以识别这些URL? 最佳答案 在Rails2.x中尝试:map.resources:stories,:collection=>{:top=>:get,:latest=>:get}在Rails3.x中:resources:storiesdocollectiondoget'top'ge

ruby-on-rails - rails : Why images are not showing in my rails basic app

我的index.html.erb代码-Listingproducts'list_image')%>图像在app\assets\images下..但是前端没有显示静态图像。当我Firebug它时,我相信图像标签是正确形成的...让我知道我在这部分缺少什么。截图-图片也到位了-让我知道我做错了什么,我该如何解决。编辑github.com/swapnesh/depot请告诉我为什么它在我的案例中不起作用。尽管更改/images/product1.jpgTo/assets/product1.jpg使其正常工作。 最佳答案 如果您正在使用As

ruby-on-rails - 使用 define_method 动态定义 setter 方法?

我使用大量迭代来定义模型中的便捷方法,例如:PET_NAMES.eachdo|pn|define_method(pn)do......end但我从来没有能够动态定义setter方法,即:defpet_name=(name)...end像这样使用define_method:define_method("pet_name=(name)")do...end有什么想法吗?提前致谢。 最佳答案 这是一个在用于扩展类的模块中使用define_method的完整示例:moduleVerboseSetterdefmake_verbose_sette

ruby-on-rails - 无方法错误 : undefined method `on' for main:Object

当我尝试bundleexeccapproductiondeploy--trace时,我收到一条错误消息:deploy@h2540559:/www/apps/foodsoft$bundleexeccapproductiondeploy--trace**Invokeproduction(first_time)**Executeproduction**Invokeload:defaults(first_time)**Executeload:defaults**Invokervm:hook(first_time)**Executervm:hookcapaborted!NoMethodError

ruby-on-rails - rails : creating a custom data type/creating a shorthand

我想知道如何创建自定义数据类型以在rake迁移文件中使用。示例:如果您要创建模型,则可以在迁移文件中添加列。它可能看起来像这样:defself.upcreate_table:productsdo|t|t.column:name,:stringt.timestampsendend我想知道如何创建这样的东西:t.column:name,:my_custom_data_type之所以这样创建,例如一个“货币”类型,无非是一个精度为8,小数位数为2的小数。由于我只使用MySQL,所以这个数据库的解决方案已经足够了。感谢您的反馈和意见! 最佳答案

ruby - 什么时候用undef_method,什么时候用remove_method?

我想重新定义一个方法,但要避免与之相关的警告。我应该使用undef_method还是remove_method来这样做?(是的,重新定义方法有点老套。我这样做是因为我有一些内存,我想在单元测试运行时使用,而不是在程序本身运行时使用。) 最佳答案 来自finemanual:undef_method(symbol)→selfPreventsthecurrentclassfromrespondingtocallstothenamedmethod.Contrastthiswithremove_method,whichdeletestheme

ruby-on-rails - 无方法错误 : undefined method `safe_constantize'

我有一个简单的模型:classPost它有一个名为type的INT列当我创建记录时:Post.create(:type=>1)我得到:NoMethodError:undefinedmethod`safe_constantize'for1:Fixnum我做错了什么? 最佳答案 就像这样:在Fixnum上没有名为safe_constantize的方法。type用于SingleTableInheritance.您通常会在应用程序中将已知模型的字符串表示形式作为type值。参见railsguides

ruby-on-rails - 切换到 Rails 4.1 时出现错误 - `method_missing':ActiveRecord::Base:Class (NoMethodError) 的未定义方法 `whitelist_attributes='

从Rails4.0切换到Rails4.1时出现此错误:activerecord-4.1.8/lib/active_record/dynamic_matchers.rb:26:in`method_missing':undefinedmethod`whitelist_attributes='forActiveRecord::Base:Class(NoMethodError)我没有在我的应用程序的任何地方使用attr_accessible或attr_protected所以我想知道为什么我有问题。当迁移到Rails4.0时,我已经安装了我的application.rb:配置/应用程序.rb:c

ruby-on-rails - rails : Copying attributes from an object to another using the "attributes" method

让模型Quote具有属性[price,description]让模型Invoice有属性[price,description,priority]让invoice模型Invoice中的对象具有属性{price:10,description:'lamp',priority:10}invoice={price:10,description:'lamp',priority:10}假设我想将invoice属性复制到新的quote。quote=Quote.new(invoice.attributes)这会引发一个错误,即priority在模型Quote中不存在。如何将invoice属性复制到新的q

Ruby 单例方法 (class_eval, define_method) vs (instance_eval, define_method)

Ruby中的元编程很棒,因为我经常使用它来模拟基于原型(prototype)的编程,并快速编写一些问题的原型(prototype)解决方案来测试它们的可行性。所以我想知道下面这段代码是否有本质区别:(class和(class代码的两个版本都定义了一个单例方法,我还没有遇到任何迫使我选择(instance_eval,define_method)组合而不是(class_eval,define_method)组合来定义单例方法,我想知道两者之间是否存在一些本质区别。 最佳答案 define_method没有区别。但是当您使用def时会有所