Tag: ruby

rspec失败错误:假设错误响应`false?`

我正在运行这部分测试: describe Dictionary do before do @d = Dictionary.new end it ‘can check whether a given keyword exists’ do @d.include?(‘fish’).should be_false end 使用此代码: class Dictionary def initialize @hash = {} end def add(new_entry) new_entry.class == String ? @hash[new_entry] = nil : new_entry.each { |noun, definition| @hash[noun] = definition} end def entries @hash end def keywords […]

Ruby块到字符串而不是执行

举个例子: write_as_string { puts ‘x’ } 然后我希望能够做到 def write_as_string(&block) puts block.to_s end 当我执行此操作时,我希望输出为: “puts ‘x'” 我希望能够接收块并获取块的实际代码而不是执行它。 动机:创建一个DSL,我希望将模拟转换为许多其他方法调用,隐藏在调用代码中 – 使用现有的对象和方法,而不需要猴子修补它们。 任何想法都会很棒! 谢谢 本

如何使用nokogirivalidationXHTML?

我发现一些post暗示你可以使用nokogiri gemvalidationXHTML对其DTD的影响。 虽然我已成功使用它来成功解析XHTML(寻找’a’标签等),但我正在努力validation文档。 对我来说,这个: doc = Nokogiri::XML(Net::HTTP.get(URI.parse(“http://www.w3.org”))) puts doc.validate 导致整堆: [ #, #, #, #, #, #<Nokogiri::XML::SyntaxError: No declaration for attribute profile of element head [repeat for every tag in the document.] ] 所以我假设这不是正确的方法。 我似乎无法找到任何好的例子 – 任何人都可以建议我做错了吗? 我在Mac OSX 10.5.8上运行ruby 1.8.6。 Nokogiri告诉我: nokogiri: 1.3.3 warnings: [] libxml: compiled: 2.6.23 loaded: 2.6.23 binding: extension

编辑现有的Rails迁移是个好主意?

在开始一个新项目时,模型中有很多变化,我发现很容易编辑现有的迁移并运行db:clean或db:reset不是创建新的迁移。 当app没有达到生产时我就这样做了,这意味着我可以重置/清理数据库而不用担心,我正在独自工作或者是一个小团队的一部分。 但是今天,我在Rails指南中提到了以下建议,说它不是一个好主意并且不鼓励编辑现有的迁移: 编辑现有迁移并不是一个好主意:如果已在生产计算机上运行现有版本的迁移,您将为自己和同事创建额外的工作并导致严重的麻烦。 相反,您应该编写一个新的迁移来执行您需要的更改。 编辑尚未提交到源代码控制的新生成的迁移(或者更常见的是,尚未在开发计算机之外传播的迁移)相对无害。 我想知道: 我可能遇到什么潜在的陷阱? 这些陷阱是否适用于我的案例(开发阶段,独奏工作)?

如何在一个表中添加对同一模型的多个引用的迁移? 的Ruby / Rails

如何使用引用同一个表的两个字段创建迁移? 我有表A和图像。 A.image1_id将参考图像,而A.image2_id也将参考图像。 只有2张图片,不是很多。 如果我使用 class AddFields < ActiveRecord::Migration def change change_table(:ticket) do |t| t.references :image1_id t.references :image2_id end end end 我认为这不会起作用,因为它会在最后添加另一个_id,并且可能不会知道使用’image’模型。 我也想过 change_table(:ticket) do |t| t.references :image 但是,我该如何添加其中两个呢? 我也考虑过添加 create_table :images do |t| t.belongs_to :ticket t.string :file 但我只想要2,而不是很多,这似乎不允许从票证中获取图像,如ticket.image1或ticket.image2 。 根据这个文档http://apidock.com/rails/v3.2.8/ActiveRecord/ConnectionAdapters/SchemaStatements/change_table这是我能找到的全部内容,t.references似乎也没有任何参数。 change_table(:suppliers) do |t| t.references :company end

使用Ruby将HTML转换为纯文本?

有没有什么可以将HTML转换为纯文本(也许是nokogiri脚本)? 可以保持换行的东西,但就是这样。 如果我在googledocs上写一些东西,像这样 ,并运行该命令,它输出(删除css和javascript),这: \n\n\n\n\nh1. Test h2. HELLO THEREI am some teexton the next line!!!OKAY!#*!)$! 所以格式化都搞砸了。 我相信有人已经在某处解决了这些细节。

RVM无法在Snow Leopard上安装Ruby 1.9.2:’运行’make’时出错

当我尝试使用以下方法在Snow Leopard上安装Ruby 1.9.2时: rvm install 1.9.2 我得到以下内容: ERROR: Error running ‘make ‘, please read /Users/mary/.rvm/log/ruby-1.9.2-p180/make.log ERROR: There has been an error while running make. Halting the installation. 所以,我查看make.log。 它的结尾是: readline.c: In function ‘username_completion_proc_call’: readline.c:1386: error: ‘username_completion_function’ undeclared (first use in this function) readline.c:1386: error: (Each undeclared identifier is reported only once readline.c:1386: error: for each […]

Rails3:将范围与OR结合

我需要将名称范围与运算符组合……类似于: class Product < ActiveRecord::Base belongs_to :client scope :name_a, where("products.name = 'a'") scope :client_b, joins(:client).where("clients.name = 'b'") scope :name_a_or_b, name_a.or(client_b) end 谢谢

预加载与动态条件的关联

我有Place模型和Event模型。 地方可以 在特定日期举办活动。 如何在没有N + 1查询问题的特定日期设置我的关联和查找器来加载所有地方,包括(急切加载)他们的事件? 我尝试过的: class Place has_many :events end Place.all.preload(:events).where(“events.start_date > ‘#{time_in_the_future}'”) #ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: missing FROM-clause entry for table “events”. Place.all.includes(:events).where(“events.start_date > ‘#{time_in_the_future}'”).references(:event) # only loads places that have an event at the specific date and not all places including their events (if there are any events). 我成功地想出了一个能够做我想要的但不是动态的关联(不接受参数) class Place has_many […]

为什么空格会影响ruby函数调用?

我在这段代码中遇到语法错误 render json: { “what” => “created”, “whatCreated” => “thing”, “htmlOutput” => render_to_string (partial: “some_partial”) } 但是使用这段代码我不会: render json: { “what” => “created”, “whatCreated” => “thing”, “htmlOutput” => render_to_string(partial: “some_partial”) } render_to_string之后如何破坏我的rails应用程序?