草庐IT

php - 在自定义帖子类型存档的模板中设置 Yoast 标题

全部标签

ruby-on-rails - ApplicationController :Class 的未定义方法 `caches_action'

我正在尝试升级到Rails4beta1,但遇到了一些问题。简而言之,这就是我的应用程序Controller的样子。classApplicationControllercaches_action在Rails4中移到了它自己的gem中,因此包含gem应该可以解决问题。gem"actionpack-action_caching",github:"rails/actionpack-action_caching"但是当我运行我的请求规范或在浏览器中访问该应用程序时,我收到此错误。app/controllers/application_controller.rb:3:in`':undefinedm

ruby-on-rails - 使用 before_save 回调或自定义验证器添加验证错误?

我有一个模型Listingbelongs_to:user。或者,Userhas_many:listings。每个列表都有一个对其进行分类的类别字段(狗、猫等)。User还有一个名为is_premium的bool字段。这是我验证类别的方式...validates_format_of:category,:with=>/(dogs|cats|birds|tigers|lions|rhinos)/,:message=>'isincorrect'假设我只想让高级用户能够添加老虎、狮子和犀牛。我该怎么做?最好在before_save方法中执行此操作吗?before_save:premium_che

ruby-on-rails - 每个 Ruby 未定义的方法

破败。一个人可以有很多出价,这个人只有一个出价。在我的索引操作中,我有@bids=Bid.find_by_person_id(params[:person_id])在我看来是的我得到NoMethodError:undefinedmethodeach'for#在访问人员出价的索引View时。这是因为这个人只有一个出价吗?我觉得不是这样,但除此之外我很茫然.. 最佳答案 find_by返回第一项。我想你在找Bid.where(person_id:params[:person_id]) 关于r

ruby-on-rails - 如何在 ActionController::TestCase 请求中设置内容类型

我试图像这样在我的测试用例中执行获取:request.env['CONTENT_TYPE']='application/json'get:index,:application_name=>"Heka"虽然,它失败了:ActionView::MissingTemplate:Missingtemplatealarm_events/indexwith{:handlers=>[:builder,:haml,:erb,:rjs,:rhtml,:rxml],:locale=>[:en,:en],:formats=>[:html]尽管在我的Controller中我有:respond_to:html,

ruby - 读取 zip 存档中的文件,无需解压缩存档

我有一个包含100多个zip文件的目录,我需要读取zip文件中的文件以进行一些数据处理,而无需解压缩存档。是否有一个Ruby库可以在不解压缩文件的情况下读取zip存档中的文件内容?使用rubyzip报错:require'zip'Zip::File.open('my_zip.zip')do|zip_file|#Handleentriesonebyonezip_file.eachdo|entry|#Extracttofile/directory/symlinkputs"Extracting#{entry.name}"entry.extract('here')#Readintomemoryc

Ruby 自定义字符串排序

输入字符串:1654AaBcDddeeFF输出字符串:1456acddeeABDFF我试过的代码:test_array=[]'1654AaBcDddeeFF'.each_bytedo|char|test_array1456ABDFFacddee但我希望最后看到大写字符。 最佳答案 这个呢?p'1654AaBcDddeeFF'.each_char.sort_by(&:swapcase).join#=>"1456acddeeABDFF"编辑:正如@CarySwoveland指出的那样,.chars只是.each_char.to_a的快捷

ruby-on-rails - factory_girl/rspec2 场景中的未定义方法 `each'

我正在尝试制作与投票相关的帖子。这样Post.votes就会生成与之关联的投票。Factory.define:voted_post,:parent=>:post,:class=>Postdo|p|p.association:votes,:factory=>:voteend我的rspec2相对简单:describe"votescores"doit"shouldshowmethetotalvotescore"do@post=Factory(:voted_post)@post.vote_score.should==1endend那么为什么会返回这个错误:Failures:1)Postvote

ruby-on-rails - 在条件路由期间未在 Rails 3 中设置 request.subdomain

我正在尝试根据RyanBatesscreencastonsubdomains在Rails3中设置子域.但是它对我不起作用。我有以下设置:#routes.rbconstraints(Subdomain)doget'devices'=>'devices#all'end#lib/subdomain.rbclassSubdomaindefself.matches?(request)#binding.pryrequest.subdomain.present?&&request.subdomain=="admin"endend加载urladmin.localhost:3000/devices应该将

ruby - 类型错误 : can't convert String into Integer

我有代码:classScenedefinitialize(number)@number=numberendattr_reader:numberendscenes=[Scene.new("one"),Scene.new("one"),Scene.new("two"),Scene.new("one")]groups=scenes.inject({})do|new_hash,scene|new_hash[scene.number]=[]ifnew_hash[scene.number].nil?new_hash[scene.number]当我启动它时出现错误:freq.rb:11:in`[]'

ruby - 在 Ruby 中,如果我们定义了一个方法调用 "c=",为什么 c = 3 不能调用它?

例如,如果我们defc=(foo)p"hello"endc=3c=(3)并且不会打印“hello”。我知道它可以被self.c=3调用,但为什么呢?可以通过哪些其他方式调用它? 最佳答案 c=3(和c=(3),完全等同于它)总是被解释为局部变量赋值。你可能会说只有当方法c=没有在self上定义时,它才应该被解释为局部变量赋值,但是这有很多问题:至少MRI需要在解析时知道在给定范围内定义了哪些局部变量。但是,在解析时并不知道给定的方法是否已定义。所以ruby​​直到运行时才知道c=3是否定义了变量c或者调用了方法c=,这意味着它不会知