草庐IT

in_string

全部标签

ruby-on-rails - 路由错误 : Uninitialized constant in Rails 3

我有一个包含类似这样的辅助类的文件:应用程序/类/myfile.rbModulemymoduleclassmyclass#blahblahendend我想在Controller中使用这个类,所以我写了这样的东西:require'myfile'classMyControllerController的路由定义如下:match'mycontroller',:to=>'mycontroller#index'现在是我面临的奇怪行为。它在服务器启动后的第一次运行中工作得很好。但是,当我刷新页面或再次点击URL时,出现以下错误。RoutingErroruninitializedconstantMyC

ruby-on-rails - Ruby -::in 类名

我正在处理一些遗留的RoR代码,它有四个相关的类,每个类都在自己的文件中定义。所有这些类都是解析器类,位于app/models/parsers中。每个文件名都以_parser.rb结尾。这是来自文件adf_parser.rb的示例类定义行:classParsers::AdfParser我不知道Parsers::的一部分在做什么。在我能找到的任何地方都没有定义名为Parsers的显式模块。我没有看到任何关于通过将模块规范添加到类名来隐式创建模块的文档。唯一的外部依赖是“需要'csv'”。类定义中有include语句,但我认为它们没有任何可以解释类名的内容。我创建了一个新的RoR测试项目并

ruby - 是否可以使用 & : (ampersand colon) notation with a parameter or with chaining in Ruby?

这个问题在这里已经有了答案:Canyousupplyargumentstothemap(&:method)syntaxinRuby?(9个回答)关闭8年前。我想做这样的事情:[1,2,3].map(&:to_s(2))此外,如何做类似的事情:[1,2,3].map(&:to_s(2).rjust(8,'0'))?

ruby-on-rails - 重定向到某个页面而不是 :notice when non-confirmed user logs in

如何将登录到特定页面的未确认用户重定向到某个页面而不是显示:notice(“您必须确认您的帐户”)。使用Devisegem(最新)这个问题:(Devise-Redirectedtopageifaccountisn'tconfirmed)提供了这个解决方案:#config/initializers/my_strategy.rbWarden::Strategies.add(:my_strategy)dodefvalid?trueenddefauthenticate!u=User.find_for_authentication(:email=>params[:email])ifu.nil?|

ruby-on-rails - Form_for "First argument in form cannot contain nil or be empty"错误

我不明白为什么会收到此错误,也不知道它的确切含义。Firstargumentinformcannotcontainnilorbeempty(Line3)添加新帖子//ErrorhereController:classPostsController"Yourpostwassaved"elserender"new"endenddefeditenddefupdateenddefdestroyendend 最佳答案 假设您从PostsController渲染它并使用传统的View名称,您的new方法应该创建一个新的Post并分配给它:def

ruby - Codecademy "converting between symbols and strings" ruby 类(class)

这些是Codecademy的说明:Wehaveanarrayofstringswe'dliketolateruseashashkeys,butwe'drathertheybesymbols.Createanewarray,symbols.Use.eachtoiterateoverthestringsarrayandconverteachstringtoasymbol,addingthosesymbolstosymbols.这是我写的代码(提供了strings数组):strings=["HTML","CSS","JavaScript","Python","Ruby"]symbols=[]

ruby - 在 Ruby 中,String() 和 #to_s 有什么区别

String(1.1)==(1.1).to_s=>trueString(1.1)===(1.1).to_s=>true这两种强制方式有区别吗?如果可以,你能演示一下吗? 最佳答案 docsfortheStringmethod说:ConvertsargtoaStringbycallingitsto_smethod.所以通常它们是相同的,但也有一些差异——尽管您不太可能真正看到它们。String()检查其参数的类,如果它还不是String,则对其调用to_s。直接调用to_s意味着无论如何都会调用该方法。考虑类:classMyStrin

ruby-on-rails - 为什么 String#gsub 的内容加倍?

s="#main='quotes's.gsub"'","\\'"#=>"#main=quotes'quotes"这似乎是错误的,我希望得到"#main=\\'quotes\\'"当我不使用转义字符时,它会按预期工作。s.gsub"'","*"#=>"#main=*quotes*"所以一定有转义的事情。使用ruby1.9.2p290我需要用反斜杠和引号替换单引号。更多的不一致:"\\'".length#=>2"\\*".length#=>2#Asexpected"'".gsub("'","\\*").length#=>2"'a'".gsub("'","\\*")#=>"\\*a\\*"(

ruby - 是否有 Ruby string#blank?方法?

String#blank?非常有用,但存在于Rails中,而不存在于Ruby中。Ruby中是否有类似的东西来替换:str.nil?||str.empty? 最佳答案 据我所知,普通Ruby中没有这样的东西。您可以像这样创建自己的:classNilClassdefblank?trueendendclassStringdefblank?self.strip.empty?endend这将适用于nil.blank?和a_string.blank?您可以将其扩展(就像rails一样)用于true/false和一般对象:classFalseCl

ruby-on-rails - 事件管理员 :select drop-down defaults to current value in development but defaults to blank in production

我有以下ActiveAdmin表单:formdo|f|f.inputs"TimesheetDetails"dof.input:jobs_assigned_worker,:label=>"Worker",as::select,collection:Worker.allf.input:worked_time_hours,:label=>"WorkedTime(Hours)"f.input:worked_time_mins,:label=>"WorkedTime(Minutes)"f.input:driving_time_hours,:label=>"DrivingTime(Hours)"f