草庐IT

DAY_OF_WEEK

全部标签

ruby-on-rails - rails : getting data from a table for each iteraction of a loop

我有一个循环(针对@dataset中的项目),我希望在每次迭代中从另一个表中获取不同的数据并进行一些将在View中打印的操作。我无法从循环中使用的数据集中获取此数据。如何根据MVC执行此操作?我可以将代码放入循环中,在View中,但我认为这太可怕了。我必须使用助手来执行此操作,并从View中调用该函数吗? 最佳答案 如果你有一个表,想从另一个表中获取数据,通常是在has_many关系的情况下。例如,我们有@people(Person模型),每个人都有has_many地址(Address模型)。在这些情况下,最好的办法就是这样做#Co

ruby 正则表达式 : match and get position(s) of

我想匹配一个正则表达式并获取匹配字符串中的位置例如,"AustinTexasDallasTexas".match_with_posn/(Texas)/我想要match_with_posn返回类似:[6,17]其中6和17是单词Texas的两个实例的起始位置。有这样的吗? 最佳答案 使用Ruby1.8.6+,你可以这样做:require'enumerator'#Onlyfor1.8.6,newerversionsshouldnotneedthis.s="AustinTexasDallasTexas"positions=s.enum_f

ruby-on-rails - Rails 迁移 : tried to change the type of column from string to integer

我使用railsgeneratemigrations命令在我的rails应用程序中创建了一个表。这是迁移文件:classCreateListings然后我想将纬度和经度存储为整数我试着跑:railsgeneratemigrationchangeColumnType该文件的内容是:classChangeColumnType我原以为列类型会发生变化,但是rake被中止并出现了以下错误消息。我想知道为什么这没有通过?我在我的应用程序中使用postgresql。rakedb:migrate==ChangeColumnType:migrating=========================

ruby-on-rails - rails : path of file

我在app中有一个名为csv的目录,在这个目录中我有一个名为names.csv的文件我想使用File.read(path:string)函数来读取文件。文件的相对路径是什么? 最佳答案 file=File.join(Rails.root,'app','csv','names.csv')File.read(file) 关于ruby-on-rails-rails:pathoffile,我们在StackOverflow上找到一个类似的问题: https://stac

ruby-on-rails - 为什么 Ruby on Rails 使用 http ://0. 0.0.0 :3000 instead of http://localhost:3000?

当我尝试按照官方“入门”RubyonRails教程进行操作时,很快就出错了。基本上它说:…navigatetohttp://localhost:3000.YoushouldseeRails’defaultinformationpage.但是当我按照说明操作时,我得到了=>Rails2.3.4applicationstartingonhttp://0.0.0.0:3000在尝试了这两个地址之后,我知道它们指向同一件事,但是有人可以向我解释为什么RubyonRails使用http://0.0.0.0:3000而不是http://localhost:3000?有没有办法让WEBrick服务器

ruby-on-rails - 测试 : how to focus on behavior instead of implementation without losing speed?

似乎有两种完全不同的测试方法,我想引用它们。问题是,这些意见是在5年前(2007年)提出的,我很感兴趣,从那以后发生了什么变化,我应该走哪条路。BrandonKeepers:Thetheoryisthattestsaresupposedtobeagnosticoftheimplementation.Thisleadstolessbrittletestsandactuallyteststheoutcome(orbehavior).WithRSpec,Ifeellikethecommonapproachofcompletelymockingyourmodelstotestyourcontr

ruby-on-rails - 类型错误 : no implicit conversion of Symbol into Integer

我在尝试更改散列的值时遇到了一个奇怪的问题。我有以下设置:myHash={company_name:"MyCompany",street:"Mainstreet",postcode:"1234",city:"MyCity",free_seats:"3"}defcleanupstringstring.titleizeenddefformatoutput=Hash.newmyHash.eachdo|item|item[:company_name]=cleanup(item[:company_name])item[:street]=cleanup(item[:street])output当我

ruby - 强制 bundle 安装使用 https ://instead of git://for GitHub-based gems

我正在尝试构建一个Rails项目,因为我正在使用的主机无法访问Internet的git://协议(protocol)(端口9418),我收到如下错误Fetchinggit://github.com/pivotal/jasmine.gitfatal:unabletoconnecttogithub.com:github.com[0:192.30.252.130]:errno=Connectionrefused运行bundleinstall时。GemFile中的相关行没有指定git://作为协议(protocol),它只是指向GitHub作为gem的源gem'jasmine',:github

ruby - 警告 : Can't verify CSRF token authenticity in case of API development

我现在正在使用RubyonRails开发网络API。当Rails应用程序收到没有任何csrftoken的POST请求时,将出现以下错误消息。因为该应用没有View。WARNING:Can'tverifyCSRFtokenauthenticity所以我的问题是在这种情况下如何安全地逃避csrftoken检查?非常感谢您。 最佳答案 你可以通过添加skip_before_filter:verify_authenticity_token到你的Controller。这样,所有传入Controller的请求都会跳过:verify_authen

ruby - "wrong number of arguments (1 for 0)"在 Ruby 中是什么意思?

“参数错误:参数数量错误(1代表0)”是什么意思? 最佳答案 当您定义一个函数时,您还定义了该函数需要工作的信息(参数)。如果它被设计为在没有任何额外信息的情况下工作,并且你传递了一些信息,你就会得到那个错误。例子:不接受参数:defdogend接受参数:defcat(name)end当你调用它们时,你需要用你定义的参数来调用它们。dog#worksfinecat("Fluffy")#worksfinedog("Fido")#ReturnsArgumentError(1for0)cat#ReturnsArgumentError(0f