草庐IT

find_first_of

全部标签

ruby 正则表达式 : capture the path of url

我想从任何URL中提取其路径。例如:网址:https://stackoverflow.com/questions/ask路径:questions/ask应该不难:url[/(?:\w{2,}\/).+/]但我认为我使用了错误的模式来“忽略这个”('?:'-不起作用)。什么是正确的方法? 最佳答案 我建议您不要使用正则表达式来执行此操作,而是使用内置的URI库:require'uri'uri=URI::parse('http://stackoverflow.com/questions/ask')putsuri.path#results

ruby-on-rails - 让 find_by in rails 返回多个结果

我正在尝试返回用户创建的所有组。所有组都与用户标识相关联。当我运行find_by查询时,它只返回第一个结果。有没有办法让它返回多个?提前致谢 最佳答案 我正在写一个单独的答案,因为我没有50分,无法评论JamesLowrey的答案。find_all_by已弃用(Ruby4.2)。要从模型中获取事件记录列表,请执行以下操作:Model.where(attribute_name:val)例如,要在Vehicle表(具有列名称“model_name”)中查找所有记录,使得model_name的值为“Audi”,执行@vehicles=Ve

ruby - 导轨 : Advantages of storing session in database?

我只是想知道为什么将session存储在数据库中?将session存储在数据库中有什么好处吗? 最佳答案 数据库或memcached的优势在于session数据无法在客户端被篡改,并且您可以存储比使用cookie(4kB)更大的数据量。如果您的session存储在cookie或数据库中,并且重新启动网络服务,则session数据不会丢失。只有存储在memcached中才可能丢失。如果服务器是负载平衡的,那么session数据将传递到为请求提供服务的Web服务器,因此这不是cookie、数据库或内存缓存session的问题。cooki

Ruby 1.9、YAML 和字符串编码 : how to lead a life of sanity?

在我看来,ruby1.9附带的YAML库是编码失聪的。这意味着在生成YAML时,它将采用任何字节串,并转义任何不输出干净ASCII的字节序列。这很蹩脚,但可以接受。我的问题恰恰相反。从所述YAML转储加载内容时。在下面的示例中,我创建了一个UTF-8字符串,将其转储为!binary类型。当我加载它时,它的编码是ASCII-8BIT。在示例的最后,我尝试将原始字符串和重新加载的字符串与另一个UTF-8字符串连接起来。后者将因Encoding::CompatibilityError而失败。require'yaml's0="Iñtërnâtiônàlizætiøn"y=s0.to_yamls

ruby - Rspec 模拟错误 : wrong number of arguments

我正在尝试使用Rspec对StripeAPI进行stub,但我遇到了一个问题。这是我的代码的样子:Stripe::Customer.should_receive(:create).with(any_args).and_raise(Stripe::CardError)这是我遇到的错误:Failure/Error:Stripe::Customer.should_receive(:create).with(any_args).and_raise(Stripe::CardError)ArgumentError:wrongnumberofarguments(0for3..6)

ruby-on-rails - 助手设计 : could not find the `Warden::Proxy` instance on request environment

我尝试将Devise用于我的Rails应用程序。我可以注册并登录,但是当我转到其他页面“构建”时,出现以下错误:Devise::MissingWardeninHome#showDevisecouldnotfindtheWarden::Proxyinstanceonyourrequestenvironment.MakesurethatyourapplicationisloadingDeviseandWardenasexpectedandthattheWarden::Managermiddlewareispresentinyourmiddlewarestack.Ifyouareseeing

ruby-on-rails - 为什么我不想到处使用 inverse_of?

如这里所述:http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.htmlinverse_of似乎告诉Rails缓存内存关联并最小化数据库查询。他们的例子是:classDungeon:dungeonhas_one:evil_wizard,:inverse_of=>:dungeonendclassTrap:trapsend他们立即跟进:for`belongs_to`associations`has_many`inverseassociationsareignored.所以我有几个问题。has_m

ruby-on-rails - cucumber + capybara : Problem with a scenario that redirects the browser outside of my app

GivenIhavearailsappAndI'musingcucumberAndI'musingcapybaraAndIhaveanactionthatresultsinaredirect_to"http://some.other.domain.com/some_path"WhenItestthisactionThenthein-appportionofthetestworksfineButIseethiserror:Noroutematches"/some_path"with{:method=>:get}(ActionController::RoutingError)所以capyb

ruby - ruby 中的最后一个索引是否有等效的 Array#find_index ?

Array#find_index允许您找到第一个项目的索引等于一个对象,或者使传递给它的block评估为真Array#rindex可以让您找到等于object的最后一项的索引,但是有没有什么可以让您找到的索引使block传递给它的最后一项返回true?否则,我是否应该做类似的事情last_index=array.length-1-array.reverse.find_index{|item|item.is_wanted?} 最佳答案 在Ruby1.9.2中Array#rindex接受block:http://apidock.com/

ruby-on-rails - rails 3 : validate presence of at least one has many through association item

我有两个模型:Project和ProjectDiscipline:classProject:destroyhas_many:project_disciplines,through::project_disciplinizationsattr_accessible:project_discipline_idsattr_accessible:project_disciplines_attributesaccepts_nested_attributes_for:project_disciplines,:reject_if=>proc{|attributes|attributes['name'