草庐IT

ruby-on-rails - Scaffold Rails 3 View 中的外键下拉列表

我使用脚手架和Rails3创建了2个模型。模型是位置和作业,每个作业都有一个位置。我在脚手架生成代码中创建了所需的引用调用,但是当我查看创建新作业的View时,我看到的只是一个文本框,我应该在其中添加location_id。我怎样才能让它变成下拉菜单以获得更好的用户体验? 最佳答案 想象一下,您有每个位置的titleAPI:http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select 关于r

ruby-on-rails - 将 Ruby 代码和文字标记与 Haml 混合

如何用HAML编写这个ERB:#OR我可以:=some_ruby_code+":"#and=some_ruby_code%br但我不想在这里连接,我想将它写成内联:(=some_ruby_code):#and(=some_ruby_code)%br 最佳答案 =some_ruby_code+":"-#and=some_ruby_code+""编辑1:我不确定您在寻找什么。你想要其中之一吗?==#{some_ruby_code}:-#and==#{some_ruby_code}或==#{some_ruby_code}:-#and=so

ruby-on-rails - 在 Rails 3 中调用 id 为 nil

在开发模式下:nil.id=>"Calledidfornil,whichwouldmistakenlybe4--ifyoureallywantedtheidofnil,useobject_id"在生产模式中:nil.id=>4为什么? 最佳答案 在您的环境配置中查找包含以下内容的行:#Logerrormessageswhenyouaccidentallycallmethodsonnil.config.whiny_nils=true#orfalseinproduction.rb这是为了防止您在开发模式下调用nil上的方法。我猜他们在生

ruby-on-rails - 如何将 'Fixnum' 转换为 Rails 3.1.3 中的日期?

我正在尝试将此输出显示为日期:1296524384但是当我在上面调用.to_date时,出现了这个错误:undefinedmethod`to_date'for1296524384:Fixnum 最佳答案 你可以这样做:the_time=Time.at(1296524384)这给你:2011-01-3120:39:44-0500顺便说一句,1296524384指的是UNIX或纪元时间。它测量自1970年1月1日以来经过的秒数。为了更好地格式化它,您可以使用Ruby的strftime方法。the_time=Time.at(1296524

ruby-on-rails - 限制 will_paginate 中的页数

因此,在使用Sphinx时,搜索限制为1000个结果。但是,如果will_paginate生成的结果分页链接超过1000个,请不要考虑这一点,并提供指向超过1000/per_page的页面的链接。设置最大页数或类似内容的明显方法是什么?干杯。 最佳答案 我认为最好将参数:total_entries提交给方法paginate:@posts=Post.paginate(:page=>params[:page],:per_page=>30,:total_entries=>1000)will_paginate将仅为显示1000个结果所需的页

ruby-on-rails - 如何将此 Ruby 字符串转换为数组?

将以下Ruby字符串转换为数组的最佳方法是什么(我使用的是ruby​​1.9.2/Rails3.0.11)Rails控制台:>Item.first.ingredients=>"[\"Bread,wholewheat,100%,slice\",\"EggSubstitute\",\"new,Eggs,scrambled\"]">Item.first.ingredients.class.name=>"String">Item.first.ingredients.length77期望的输出:>Item.first.ingredients_a["Bread,wholewheat,100%,sl

ruby-on-rails - 使用 ruby​​ on rails 在 json 中发送错误消息

我正在验证ruby​​onrails中的输入字段。我检查用户是否输入或填写了这些字段。如果假设name字段未填写,则向用户发送一条错误消息,指示name字段未填写。其他错误也是如此。我如何使用ruby​​onrails在json中发送这种消息。这是我现在正在做的。这个模型validates:email,:name,:company,:presence=>truevalidates_format_of:email,:with=>/\A[a-z0-9!#\$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#\$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9

ruby-on-rails - 特征未注册 : attribute name

完成这个有困难。我正在使用seed.rb+factory_girl来使用rakedb:seed填充数据库。(我知道固定装置存在,但我想以这种方式完成,这只是一个示例,数据库将填充复杂的关联对象。)我的种子.rb:require'factory_girl_rails'["QM","CDC","SI","QS"].eachdo|n|FactoryGirl.create(:grau,nome:n)end还有我的/factories/graus.rbFactoryGirl.definedofactory:graudonomeendend但是当我运行时:rakedb:seed我得到:rakeab

ruby-on-rails - Rubygems - 包在哪里下载?

当你安装一个新包时,例如,'geminstallfb-graph',文件下载到哪里了? 最佳答案 使用此命令查找特定gem的安装位置:gemwhich例如:gemwhichfb-graph 关于ruby-on-rails-Rubygems-包在哪里下载?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/13200065/

ruby-on-rails - Rspec 测试属于并且有很多

我正在运行rspec测试以确保两个模型通过has_many和belongs_to相互关联。下面是我的测试。describe"testingforhasmanylinks"dobeforedo@post=Post.new(day:"Day1",content:"Test")@link=Link.new(post_id:@post.id,title:"google",url:"google.com")endit"inthepostmodel"do@post.links.first.url.should=="google.com"endend测试告诉我url是一个未定义的方法。我的测试有什么