草庐IT

type_param

全部标签

ruby-on-rails - 类型错误 : wrong argument type String (expected Module)

我有以下代码:classProfileLookup基本上包含大量查找数据,按类别拆分。目的是为数据库中的每个类别创建一个方法。通过Rails控制台,此代码按预期工作:ruby-1.9.3@hub:002>ProfileLookup.available_gendersProfileLookupLoad(0.6ms)SELECT"profile_lookups".*FROM"profile_lookups"WHERE"profile_lookups"."category"='gender'ORDERBYvalue=>["Female","Male"]但是,我的规范不合格。以下规范:requ

ruby-on-rails - Ruby on Rails 3: Devise::LdapAdapter.get_ldap_param 未定义方法错误

我正在运行:ruby1.9.3p0,rails3.1.1,设计1.4.9,Devise_ldap_authenticatable0.4.10我正在使用Devise通过ldap服务器验证我的Rails应用程序。我使用用户名而不是电子邮件进行身份验证,所以我表中的电子邮件字段自然是空白的。在ldap中查询email,官方的做法是在用户模型中加入这段代码:before_save:get_ldap_emaildefget_ldap_emailself.email=Devise::LdapAdapter.get_ldap_param(self.username,"mail")end这段代码失败了

ruby - pg: exec_params 不替换参数?

第一次使用pggem访问postgres数据库。我已成功连接并可以使用#exec运行查询,但现在使用#exec_params构建一个简单的查询似乎没有替换参数。即:get'/databases/:db/tables/:table'do|db_name,table_name|conn=connect(db_name)query_result=conn.exec_params("SELECT*FROM$1;",[table_name])end结果为#这似乎是一个非常简单的示例-我是否从根本上误解了如何使用此方法? 最佳答案 您可以将占位

ruby - 自定义 to_yaml 和 domain_type

我需要定义用于序列化/反序列化对象的自定义方法。我想做类似下面的事情。classPersondefto_yaml_type"!example.com,2010-11-30/Person"enddefto_yaml"stringrepresentingperson"enddeffrom_yaml(yaml)Person.load_from(yaml)endend声明序列化/反序列化的正确方法是什么? 最佳答案 好的,这就是我想出的classPersondefto_yaml_type"!example.com,2010-11-30/pe

ruby-on-rails - Rails 5 Rspec 接收 ActionController::Params

我刚刚升级到Rails5。在我的规范中,我有以下内容期望(模型).toreceive(:update).with(foo:'bar')但是,由于params不再扩展Hash而现在是ActionController::Parameters规范失败了,因为with()期待一个散列,但它实际上是ActionController::Parameters是否有更好的方法在Rspec中做同样的事情,例如不同的方法with_hash?我可以使用解决这个问题expect(model).toreceive(:update).with(hash_including(foo:'bar'))但这只是检查参数是

ruby - 如何在 ruby​​ 中设置 header ['content-type' ] ='application/json'

require'net/http'require'rubygems'require'json'url=URI.parse('http://www.xyxx/abc/pqr')resp=Net::HTTP.get_response(url)#get_responsetakesanURIobjectdata=resp.bodyputsdata这是我在ruby​​中的代码,resp.data以xml形式提供给我数据。restapi默认返回xml数据,如果headercontent-type是application/json,则返回json。但我想要json格式的数据。为此我必须设置heade

ruby-on-rails - 葡萄 : required params with grape-entity

我正在用grape编写一个API服务器,我选择使用grape-entity因为它能够自动生成swagger的文档。但是现在我在按要求设置参数时遇到了问题。因为葡萄不验证参数是否存在。看起来grape忽略了实体参数的required:true。app.rbmoduleSmartmoduleVersion1classAppos_entity.rbmoduleSmartmoduleEntitiesclassOSEntityapp_entity.rbmoduleSmartmoduleEntitiesclassAppEntity现在其他一切都很好,但我不知道如何以DRY方式使用实体,并让grap

ruby - 如何在 Ruby 中检查参数 "param[:some_value]"

我知道一些检查参数是否为零的方法ifparam[:some_value]ifparam[:some_value].present?if!param[:some_value].nil?#unlessparam[:some_value].nil?if!param[:some_value].blank?#unlessparam[:some_value].blank?哪一个是正确的并且最受欢迎?它们之间有什么区别?我宁愿使用ifparam[:some_value]因为它最简单也最短。 最佳答案 以下是nil?、blank?和present?

ruby-on-rails - Rails 使用 Symbol 与 String 作为 params hash 中的键

如果我们使用字符串作为哈希键,Ruby需要计算字符串并查看它的内容(并计算其上的哈希函数)并将结果与​​已存储的键的(哈希)值进行比较在哈希中。如果我们使用一个符号作为散列键,则暗示它是不可变的,因此Ruby基本上可以将对象ID的(散列函数)与键的(散列)对象ID进行比较已经存储在哈希中。(快得多)。但问题在于Railsparams,它是HashWithIndifferentAccess的实例,如果我们编写params[:some_key]它会转换:som​​e_key到'some_key'然后它会尝试在参数散列中查找键。line159defconvert_key(key)key.ki

ruby - pg gem : 'Warning: no type cast defined for type "numeric"'

我在从pggem中获取输入结果时遇到问题。require'pg'require_relative'spec/fixtures/database'client=PG.connect(DB[:pg])client.type_map_for_queries=PG::BasicTypeMapForQueries.new(client)client.type_map_for_results=PG::BasicTypeMapForResults.new(client)client.exec(%|select*fromtestme;|)do|query|query.each{|r|putsr.ins