草庐IT

key_name

全部标签

ruby-on-rails - Rails 4 + Devise : Invalid route name, 已在使用中

我正在按照此操作方法在成功注册后修改确认页面。https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-up-(registration)我按照它所说的做了所有事情,但我得到了这个错误:in`add_route':Invalidroutename,alreadyinuse:'new_user_session'(ArgumentError)Youmayhavedefinedtworouteswiththesamenameusingthe`:as`o

ruby-on-rails - Rails 3 额外的 session 配置选项(key、expires_after、secure)

谁能指出新的Rails3.xsession配置选项是什么?我正在尝试复制我在Rails2.3.x应用程序中的相同配置。这是我在应用程序中使用的配置:#environment.rbconfig.action_controller.session_store=:active_record_storeconfig.action_controller.session={:key=>'_something',#non-securefordevelopment:secret=>'reallylongrandomstring'}#production.rb-overrideenvironment.r

关于Document mapping type name can‘t start with ‘_‘, found: [_update]

在修改elasticsearch时,用_update进行局部修改,修改失败,报错{    "error": {        "root_cause": [            {                "type": "invalid_type_name_exception",                "reason": "Document mapping type name can't start with '_', found: [_update]"            }        ],        "type": "invalid_type_name_exce

ruby - 冒号(:) appears as forward slash (/) when creating file name

我正在使用日期和时间来标记我正在创建的新文件,但是当我查看该文件时,冒号是一个正斜杠。我正在使用10.7+在Mac上开发这是我使用的代码:File.open("#{time.hour}:00,#{time.month}-#{time.day}-#{time.year}","a")do|mFile|mFile.syswrite("#{pKey}-#{tKey}:\n")mFile.syswrite("Itemsclosed:#{itemsClosed}|Totalitems:#{totalItems}|Percentclosed:%#{pClosed}\n")mFile.syswrite

用于散列 : each element the key and derive value from it 的 Ruby 数组

我有一个字符串数组,想用它来哈希。数组的每个元素都是键,我想根据该键计算值。是否有Ruby方法可以做到这一点?例如:['a','b']转换为{'a'=>'A','b'=>'B'} 最佳答案 您可以:a=['a','b']Hash[a.map{|v|[v,v.upcase]}] 关于用于散列:eachelementthekeyandderivevaluefromit的Ruby数组,我们在StackOverflow上找到一个类似的问题: https://stack

Ruby Hash .keys 和 .values,可以安全地采用相同的顺序吗?

基本的irb测试表明RubyHash以匹配顺序返回.keys和.values。假设是这种情况是否安全? 最佳答案 是的。根据RubyDocsforHash,"哈希按照插入相应键的顺序枚举它们的值。"因此,如果以相同的方式创建哈希,您应该始终获得相同的哈希顺序。 关于RubyHash.keys和.values,可以安全地采用相同的顺序吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/question

ruby-on-rails - rails : Testing named scopes with RSpec

我是测试Rails网络应用程序和RSpec的新手。我使用遗留代码并需要添加测试。那么使用RSpec测试查找器和命名范围的最佳方法是什么?我在Google中找到了一些方法,但它们并不理想。例如:http://paulsturgess.co.uk/articles/show/93-using-rspec-to-test-a-named_scope-in-ruby-on-railsit"excludesusersthatarenotactive"do@user=Factory(:user,:active=>false)User.active.should_notinclude(@user)e

ruby-on-rails - rake 路由错误 "Missing :action key on routes definition"

我得到了$rakeroutesrakeaborted!ArgumentError:Missing:actionkeyonroutesdefinition,pleasecheckyourroutes./usr/local/rvm/gems/ruby-2.1.2/gems/actionpack-4.1.5/lib/action_dispatch/routing/mapper.rb:243:in`default_controller_and_action'/usr/local/rvm/gems/ruby-2.1.2/gems/actionpack-4.1.5/lib/action_dispa

ruby - hash ['key' ] 到 Ruby 中的 hash.key

我有一个散列foo={'bar'=>'baz'}我想调用foo.bar#=>'baz'我的动机是将activerecord查询重写为原始sql查询(使用Model#find_by_sql)。这将返回以SELECT子句值作为键的散列。但是,我现有的代码依赖于object.method点表示法。我想做最少的代码重写。谢谢。编辑:看来Lua有这个功能:point={x=10,y=20}--Createnewtableprint(point["x"])--Prints10print(point.x)--Hasexactlythesamemeaningaslineabove

ruby-on-rails - 如何优雅地对 'nested' 哈希值进行 symbolize_keys

考虑以下代码:hash1={"one"=>1,"two"=>2,"three"=>3}hash2=hash1.reduce({}){|h,(k,v)|h.merge(k=>hash1)}hash3=hash2.reduce({}){|h,(k,v)|h.merge(k=>hash2)}hash4=hash3.reduce({}){|h,(k,v)|h.merge(k=>hash3)}hash4是一个“嵌套”散列,即具有字符串键和类似“嵌套”散列值的散列。Rails中Hash的'symbolize_keys'方法让我们可以轻松地将字符串键转换为符号。但我正在寻找一种优雅方法将所有键(主键