草庐IT

connect_to_database

全部标签

ruby-on-rails - rake 数据库 :reset 'user does not have CONNECT privilege'

所以我在我的Rails应用程序中使用HerokuPostgres,但我没有在Heroku本身上托管我的应用程序。我在我的database.yml中使用了来自Heroku的ActiveRecord连接详细信息,它看起来像这样:development:adapter:postgresqlencoding:unicodepool:5database:[database]username:[username]password:[password]host:ec2-54-227-243-78.compute-1.amazonaws.comport:5432但是,现在我正在尝试rakedb:mig

ruby - `respond_to?` 与 `respond_to_missing?`

与定义respond_to?相比,定义respond_to_missing?有什么意义?如果为某些类重新定义respond_to?会出什么问题? 最佳答案 没有respond_to_missing?已定义,试图通过method获取方法会失败:classFoodefmethod_missingname,*argspargsenddefrespond_to?name,include_private=falsetrueendendf=Foo.newf.bar#=>[]f.respond_to?:bar#=>truef.method:bar

ruby-on-rails - `:location => ...`格式语句中的 `head :ok`和 'respond_to'是什么意思?

我正在使用RubyonRails3,我想知道:location=>...和head:ok语句在以下代码中的含义,如何它们的工作原理以及我如何\应该使用它们。respond_todo|format|format.xml{render:xml=>@user,:status=>:created,:location=>@user}endrespond_todo|format|format.xml{head:ok}end 最佳答案 render...:location=>@user将设置HTTPlocationheader通知客户端新创建资源

ruby - to_a 和 to_ary 有什么区别?

to_a和to_ary有什么区别? 最佳答案 to_ary用于隐式转换,而to_a用于显式转换。例如:classCoordinatesattr_accessor:x,:ydefinitialize(x,y);@x,@y=x,yenddefto_a;puts'to_acalled';[x,y]enddefto_ary;puts'to_arycalled';[x,y]enddefto_s;"(#{x},#{y})"enddefinspect;"#"endendc=Coordinates.new10,20#=>#splat运算符(*)是一

ruby-on-rails - Errno::ECONNREFUSED:连接被拒绝 - connect(2) for action mailer

我长期以来一直在使用Rails。现在我在ActionMailer中遇到了一个小问题。我想在用户注册时发送一封电子邮件以确认他的注册。我可以在开发模式下发送电子邮件,但在生产模式下不能。异常Errno::ECONNREFUSED:Connectionrefused-connect(2)每次调用deliver方法时都会出现。我写了下面的代码。我的SMTP配置如下:config.action_mailer.default_url_options={:host=>"localhost:3000"}config.action_mailer.delivery_method=:smtpconfig.

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 - 为什么 Ruby 使用 respond_to?而不是responds_to?

我很好奇为什么Ruby的内省(introspection)相关方法检查对象是否响应方法是respond_to?而不是responds_to?这对我来说总是很尴尬,但也许那是因为我习惯了在objective-c中使用respondsToSelector。 最佳答案 马茨prefers第二人称单数或第三人称复数:"responds_to?"probablymakesmoresensetoEnglishspeakersthan"respond_to?".Maybe.ButI'mJapanese.RubyisnotEnglish.It'st

ruby-on-rails - ruby rails : how to get error messages from a child resource displayed?

当我呈现XML模板时,我很难理解如何让Rails为验证失败的子资源显示明确的错误消息。假设我有以下类(class):classSchool/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i,:message=>"Youmustsupplyavalidemail"end现在,在Controller中,假设我们想要构建一个简单的API来允许我们添加一个新的学校,其中有一个学生(我再说一次,这是一个糟糕的例子,但发挥它的作用是为了问题)classSchoolsController@school.errors,:status=>:unprocessable_

ruby-on-rails - rails : respond_to JSON and HTML

我有一个Controller“UserController”,它应该响应对http://localhost:3000/user/3的正常请求和ajax请求。当是正常请求时,我要渲染我的View。当是AJAX请求时,我想返回JSON。正确的方法似乎是respond_todo|format|block。编写JSON很容易,但我怎样才能让它响应HTML并像往常一样简单地呈现View呢?defshow@user=User.find(params[:id])respond_todo|format|format.html{render:show????thisseemsunnecessary.Ca

ruby - 我如何从 `require' : no such file to load in ruby? 中拯救

我正在尝试从“require”中解救出来:没有这样的文件可以按顺序加载到ruby​​中提示用户指定-I标志,以防他忘记这样做。基本上代码如下所示:beginrequire'someFile.rb'rescueputs"someFile.rbwasnotfound,haveyou"puts"forgottentospecifythe-Iflag?"exitend我原以为rescue部分会在找不到someFile.rb的情况下接管执行,但我的假设是错误的。 最佳答案 没有参数的rescue只拯救StandardErrors。LoadEr