Tag: ruby

如何在Ruby中暂时redirectstderr?

我希望在一个块的持续时间内暂时将stderrredirect到一个Ruby脚本中,以确保在块的末尾将其重置为原始值。 我很难find如何在ruby文档中做到这一点。

Ruby中方法名称的限制是什么?

例如,我发现方法名称bundler? 在下面的片段中,不知道是否是? 字符是专门的关键字或只是方法名称的一部分。 # This is a predicate useful for the doc:guides task of applications. def bundler? # Note that rake sets the cwd to the one that contains the Rakefile # being executed. File.exists?('Gemfile') end

我如何强制RAILS_ENV在耙子任务?

我有这个小耙子任务: namespace :db do namespace :test do task :reset do ENV['RAILS_ENV'] = "test" Rake::Task['db:drop'].invoke Rake::Task['db:create'].invoke Rake::Task['db:migrate'].invoke end end end 现在,当我执行时,它会忽略我尝试硬编码的RAILS_ENV。 我如何使这个任务按预期工作

Ruby 1.9:我怎样才能正确地提升和取消多字节string?

所以matz决定在Ruby 1.9.1中保持上/[AZ]/i downcase为/[AZ]/i 。 ActiveSupport::Multibyte在Ruby 1.8.x中通过String#mb_chars已经有了很好的i18n情况。 但是,在Ruby 1.9.1下试用时,它似乎不起作用。 下面是我写的一个简单的testing脚本,以及我得到的输出: $ cat test.rb # encoding: UTF-8 puts("@ #{RUBY_VERSION} " + (__ENCODING__ rescue $KCODE).to_s) sd, su = "Iñtërnâtiônàlizætiøn", "IÑTËRNÂTIÔNÀLIZÆTIØN" def ps(u, d, k); puts "%-30s: %24s / %-24s" % [k, u, d] end ps sd.upcase, su.downcase, "Plain ruby" require 'rubygems'; require 'active_support' ps sd.upcase, su.downcase, "With active_support" […]

devise秘密密钥没有设置

我正在开发一个Rails 4应用程序,使用Active Admin gem来pipe理后端。 Active Admin反过来使用Devise进行用户authentication。 现在,当我尝试在VPS服务器上使用capistrano部署应用程序时,出现以下错误: rake aborted! Devise.secret_key was not set. Please add the following to your Devise initializer: config.secret_key = '– secret key –' 谷歌search对这个错误没有太大的作用。 任何build议,为什么它是抛出一个错误? 我应该添encryption钥来devise初始值设定项,因为我找不到在initializers/devise.rb设置这样的设定项的initializers/devise.rb吗?

不推荐使用Rails 4的警告has_many

class RelatedList < ActiveRecord::Base extend Enumerize enumerize :list_type, in: %w(groups projects) belongs_to :content has_many :contents, :order => :position end 当我尝试在控制台中创buildlogging时,我的Rails应用程序中会引发此警告。 弃权警告:您的RelatedList.has_many:内容声明中的以下选项已弃用::order。 请改用一个范围块。 例如,以下内容:has_many:spam_comments,conditions:{spam:true},class_name:'Comment'应重写为:has_many:spam_comments, – > {where spam:true},class_name:'Comment'。 (来自/Users/shivam/Code/auroville/avorg/app/models/related_list.rb:7) 看来Rails 4在模型中使用了新的:order语法,但我似乎无法在Rails Guides中find文档。

Rails Model has_many与多个foreign_keys

相对较新的轨道,并试图build模一个非常简单的家庭“树”与单人模型,有一个名称,性别,father_id和mother_id(2父母)。 下面基本上是我想要做的,但显然我不能重复:has_many中的孩子(第一个被覆盖)。 class Person < ActiveRecord::Base belongs_to :father, :class_name => 'Person' belongs_to :mother, :class_name => 'Person' has_many :children, :class_name => 'Person', :foreign_key => 'mother_id' has_many :children, :class_name => 'Person', :foreign_key => 'father_id' end 是否有一个简单的方法来使用has_many与2个外键,或者也许根据对象的性别更改外键? 还是有另外一个/更好的方法呢? 谢谢!

path帮助程序使用点而不是斜线生成path

在我的routes.rb我有以下几点: resources :message_threads 当我打电话时: message_threads_path(1) 我得到: /message_threads.1 为什么是这样? 我的其他资源工作正常。 我是不是正确地复数化或者什么?

Ruby使用variables名称的正则expression式

是可以创build/使用正则expression式模式的ruby,基于variables名的值? 例如,我们都知道我们可以用Rubystring来做以下事情: str = "my string" str2 = "This is #{str}" # => "This is my string" 我想用正则expression式做同样的事情: var = "Value" str = "a test Value" str.gsub( /#{var}/, 'foo' ) # => "a test foo" 显然,这并不像上市,我只是把它作为一个例子来表明我想要做什么。 我需要根据variables内容的值进行正则expression式匹配。

在模型中使用助手:我如何包含助手依赖?

我正在写一个模型来处理来自文本区域的用户input。 遵循http://blog.caboo.se/articles/2008/8/25/sanitize-your-users-html-input的build议,在保存到数据库之前,我正在清理模型中的input,使用before_validate回电话。 我的模型的相关部分如下所示: include ActionView::Helpers::SanitizeHelper class Post < ActiveRecord::Base { before_validation :clean_input … protected def clean_input self.input = sanitize(self.input, :tags => %w(biu)) end end 不用说,这是行不通的。 当我尝试保存新post时,出现以下错误。 undefined method `white_list_sanitizer' for #<Class:0xdeadbeef> 显然,SanitizeHelper创build了一个HTML :: WhiteListSanitizer的实例,但是当我将它混合到我的模型中时,它找不到HTML :: WhiteListSanitizer。 为什么? 我能做些什么来解决这个问题?