Tag: ruby

Rails模型没有数据库

我想用ActiveRecordvalidation创build一个Rails(2.1和2.2)模型,但没有数据库表。 什么是最广泛使用的方法? 我发现一些声称提供这种function的插件,但是其中许多插件似乎没有被广泛使用或维护。 社区推荐我做什么? 现在我倾向于基于这篇博客文章提出自己的解决scheme。

Array#each与Array#map

hash = { "d" => [11, 22], "f" => [33, 44, 55] } # case 1 hash.map {|k,vs| vs.map {|v| "#{k}:#{v}"}}.join(",") => "d:11,d:22,f:33,f:44,f:55" # case 2 hash.map {|k,vs| vs.each {|v| "#{k}:#{v}"}}.join(",") => "11,22,33,44,55" 唯一的区别是情况1使用vs.map ,情况2使用vs.each 。 这里发生了什么?

如何在Rails中为同一个表单创build多个提交button?

我需要有多个提交button。 我有一个创buildContact_Call实例的表单。 一个button就像正常一样创build它。 另一个button创build它,但需要具有与默认值不同的:属性值,还需要在控制器中使用的另一个相关模型上设置该属性。 我怎么做? 我不能改变路线,那么有没有办法发送一个不同的variables,被[:params]拾取? 如果我这样做,我在控制器中做了什么,build立了一个案例陈述?

do..end vs大括号的ruby块

我有一个同事积极地说服我,我不应该使用do..end,而是使用大括号来定义Ruby中的多行块。 我坚定地只用短括弧的花括号来做一切事情。 但是我想我会联系更大的社区去解决问题。 那么这是什么,为什么? (一些应用代码的例子) context do setup { do_some_setup() } should "do somthing" do # some more code… end end 要么 context { setup { do_some_setup() } should("do somthing") { # some more code… } } 就个人而言,只要看看上面这个问题,我就可以回答这个问题,但是我想把这个问题向更大的社区开放。

Rails,MySQL和Snow Leopard

我使用WWDC上的光盘升级到Snow Leopard。 试图运行我的一些rails应用程序现在抱怨sql (in /Users/coneybeare/Projects/Ambiance/ambiance-server) !!! The bundled mysql.rb driver has been removed from Rails 2.2. Please install the mysql gem and try again: gem install mysql. Importing all sounds in /Users/coneybeare/Projects/Ambiance/ambiance-sounds/Import 32/Compressed/ — AdirondackPeepers.caf !!! The bundled mysql.rb driver has been removed from Rails 2.2. Please install the mysql gem and try again: gem […]

Ruby的dup和clone方法有什么区别?

Ruby的文档dup说: 一般来说, clone和dup在后代类中可能有不同的语义。 虽然clone用于复制对象,包括其内部状态,但dup通常使用后代对象的类来创build新实例。 但是当我做一些testing时,我发现它们实际上是一样的: class Test attr_accessor 😡 end x = Test.new xx = 7 y = x.dup z = x.clone yx => 7 zx => 7 那么这两种方法有什么区别呢?

在Ruby中不断从STDOUT中读取外部进程

我想通过ruby脚本从命令行运行blender,ruby脚本将逐行处理由blender提供的输出,以更新GUI中的进度条。 搅拌机是我需要读取的标准输出的外部过程并不重要。 当搅拌机进程仍在运行时,我似乎无法捕获搅拌机通常打印到shell的进度消息,而我尝试了几种方法。 我似乎总是在搅拌机退出后访问搅拌机的标准输出,而不是在搅拌机仍然运行的时候。 这是一个失败尝试的例子。 它确实获得并打印搅拌机输出的前25行,但只有在搅拌机过程退出后: blender = nil t = Thread.new do blender = open "| blender -b mball.blend -o //renders/ -F JPEG -x 1 -f 1" end puts "Blender is doing its job now…" 25.times { puts blender.gets} 编辑: 为了使它更清楚一些,调用blender的命令在shell中返回一个输出stream,指示进度(部分1-16完成等)。 似乎任何调用“获取”输出都被阻止,直到搅拌器退出。 问题是如何在搅拌机仍在运行时访问此输出,因为搅拌机将其输出到shell。

Bundler:在Gemfile中,require:> false是什么意思?

线是这一个: gem 'whenever', :require => false 这是否意味着gem需要安装或不需要?

为什么在Mac OS上安装Nokogiri失败,libiconv丢失?

我一直试图在Mac OS 10.9.3上安装Nokogiri,无论我尝试什么,安装失败最终以下错误消息: $ sudo gem install nokogiri — –with-xml2-include=/usr/local/Cellar/libxml2/2.9.1/include/libxml2 –with-xml2-lib=/usr/local/Cellar/libxml2/2.9.1/lib –with-xslt-dir=/usr/local/Cellar/libxslt/1.1.28 –with-iconv-include=/usr/local/Cellar/libiconv/1.14/include –with-iconv-lib=/usr/local/Cellar/libiconv/1.14/lib Building native extensions with: '–with-xml2-include=/usr/local/Cellar/libxml2/2.9.1/include/libxml2 –with-xml2-lib=/usr/local/Cellar/libxml2/2.9.1/lib –with-xslt-dir=/usr/local/Cellar/libxslt/1.1.28 –with-iconv-include=/usr/local/Cellar/libiconv/1.14/include –with-iconv-lib=/usr/local/Cellar/libiconv/1.14/lib' This could take a while… Building nokogiri using packaged libraries. ERROR: Error installing nokogiri: ERROR: Failed to build gem native extension. /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby extconf.rb –with-xml2-include=/usr/local/Cellar/libxml2/2.9.1/include/libxml2 –with-xml2-lib=/usr/local/Cellar/libxml2/2.9.1/lib –with-xslt-dir=/usr/local/Cellar/libxslt/1.1.28 –with-iconv-include=/usr/local/Cellar/libiconv/1.14/include –with-iconv-lib=/usr/local/Cellar/libiconv/1.14/lib Building nokogiri using packaged […]

收集路线和成员路线在ruby上的区别?

Rails中收集路线和成员路线有什么区别? 例如, resources :photos do member do get :preview end end 与 resources :photos do collection do get :search end end 我不明白。