草庐IT

the-game-name

全部标签

ruby-on-rails - 如何设置 ActionMailer default_url_options 的 :host dynamically to the request's hostname?

我正在尝试设置:hostforactionmailer默认url选项。我在所有环境文件中设置了以下内容config.action_mailer.default_url_options={:host=>"localhost"}我想通过提供请求主机使其更具动态性。当我尝试通过设置它时config.action_mailer.default_url_options={:host=>request.domain}或config.action_mailer.default_url_options={:host=>request.env["SERVER_NAME"]}它抛出错误...无法识别“请求

ruby-on-rails - ruby rails : How do you explicitly define plural names and singular names in Rails?

例如,我使用“Bonus”作为我的模型,所以我希望“bonuses”是复数形式而“bonus”是单数形式。但是,在Ruby中,这会导致:"bonus".pluralize#bonus"bonuses".singularize#bonuse因此,例如,当我执行“has_many:bonuses”时,它不会使用Bonus.rb模型(因为Ruby需要Bonuse.rb模型)。有没有一种方法可以在RubyonRails中以某种方式更正这一点,使“bonuses”充当模型bonus.rb的复数形式? 最佳答案 在config/initiali

ruby-on-rails - Rails : named_scope, lambda 和 block

我认为下面两个是等价的:named_scope:admin,lambda{|company_id|{:conditions=>['company_id=?',company_id]}}named_scope:admin,lambdado|company_id|{:conditions=>['company_id=?',company_id]}end但Ruby正在提示:ArgumentError:triedtocreateProcobjectwithoutablock有什么想法吗? 最佳答案 这是一个解析器问题。试试这个named_s

ruby - Vim Command-T 插件错误 : could not load the C extension

我正在尝试安装Command-T插件。我按照评论中的说明编译了vim:Installingvimwithrubysupport(+ruby)所以现在有了+ruby标志,但是当我尝试使用该插件时仍然出现此错误:command-t.vimcouldnotloadtheCextensionPleaseseeINSTALLATIONandTROUBLE-SHOOTINGinthehelpFormoreinformationtype::helpcommand-t我的系统上有ruby:>ruby-v>ruby1.8.7(2011-06-30patchlevel352)[x86_64-linux]建

ruby-on-rails - 雾警告和 AWS : unable to load the 'unf' gem

自从我上次更新包后,rails控制台(railsserver、railsconsole、db:migrate等)中的每个操作都会引发警告:[fog][WARNING]Unabletoloadthe'unf'gem.YourAWSstringsmaynotbeproperlyencoded.我确定我没有更改application.rb文件中的AWS字符串中的任何内容:#AmazonS3credentialsENV["AWS_ACCESS_KEY_ID"]="AWS_ACCESS_KEY_ID"ENV["AWS_SECRET_ACCESS_KEY"]="AWS_SECRET_ACCESS_

ruby-on-rails - rails : How to make Date strftime aware of the default locale?

我在environment.rb中将我的默认语言环境设置为de(德语)。我还看到了德语的所有错误消息,因此服务器选择了语言环境。但是当我尝试使用strftime打印日期时,如下所示:some_date.strftime('%B,%y')它以英语(January,11)打印,而不是预期的德语(Januar,11)。如何根据默认语言环境打印日期? 最佳答案 使用l(localize的别名)方法代替原始strftime,如下所示:l(date,format:'%B%d,intheyear%Y')参见here获取更多信息。您还可以定义“命名

ruby-on-rails - 如何处理 Ruby on Rails 错误 : "Please install the postgresql adapter: ` gem install activerecord-postgresql-adapter'"

运行RubyonRails(RoR)应用程序或使用ActiveRecord框架的Ruby代码,您会收到错误消息:Pleaseinstallthepostgresqladapter:geminstallactiverecord-postgresql-adapter尝试运行:geminstallactiverecord-postgresql-adapter也失败了,让你不知所措。 最佳答案 问题不是任何人写的。问题是postgresql数据库适配器的名称是“postgresql”,而不是“postgres”,尽管GEM的名称是“pg”。

Ruby 元编程 : dynamic instance variable names

假设我有以下哈希:{:foo=>'bar',:baz=>'qux'}我如何动态设置键和值以成为对象中的实例变量...classExampledefinitialize(hash)...magichappenshere...endend...这样我就可以在模型中得到以下内容...@foo='bar'@baz='qux'? 最佳答案 您要找的方法是instance_variable_set.所以:hash.each{|name,value|instance_variable_set(name,value)}或者,更简单地说,hash.e

Ruby 自定义错误类 : inheritance of the message attribute

我似乎找不到太多关于自定义异常类的信息。我所知道的你可以声明你的自定义错误类,让它继承自StandardError,这样它就可以被rescued:classMyCustomError这允许您使用以下方式提高它:raiseMyCustomError,"Amessage"稍后,在救援时收到该消息rescueMyCustomError=>eputse.message#=>"Amessage"我不知道的事我想为我的异常提供一些自定义字段,但我想从父类继承message属性。我发现阅读onthistopic@message不是异常类的实例变量,所以我担心我的继承不起作用。任何人都可以给我更多的细

ruby-on-rails - rails : Logging the entire stack trace of an exception

我一直在尝试找出记录堆栈跟踪的正确方法。我遇到了this链接指出logger.error$!,$!.backtrace是要走的路,但这对我不起作用log_error。根据文档,我看不出将第二个参数传递给错误方法是如何工作的,因为rails使用的ruby​​记录器只接受一个参数。奇怪的是(或者可能不是)第二个论点被接受了,没有任何口译员的提示。但是,我传递给它的任何内容都将被忽略。谁能解释一下我错过了什么?对错误的第二个论点是什么以及是什么吞噬了它有什么见解吗? 最佳答案 如果您查看ActiveSupport中BufferedLogg