草庐IT

dictionary-to-xml

全部标签

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 - 如何设置 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 - 使用 Ruby 解析 XML

我是使用XML的新手,但刚好有需要。我得到了一种常用的(对我而言)XML格式。标签内有冒号。1234TheName这是一个大文件,其中包含的内容远不止于此,但我希望有人会熟悉这种格式。有谁知道处理此类XML文档的方法吗?我宁愿不只是编写一种解析文本的蛮力方法,但我似乎无法使用REXML或Hpricot取得任何进展,我怀疑这是由于这些不寻常的标签。我的ruby代码:require'hpricot'xml=File.open("myfile.xml")doc=Hpricot::XML(xml)(doc/:things).eachdo|thg|['Id','Name'].eachdo|el|

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

ruby-on-rails - rails respond_to format.js API

我是一名经验丰富的JAVA和C++开发人员,我正在努力了解Rails的工作原理。我得到以下代码:respond_todo|format|if@line_item.saveformat.html{redirect_tostore_url}format.js{render:json=>@line_item,:mime_type=>Mime::Type.lookup('application/json'),:callback=>'javascriptFunction'}我一直在搜索定义我可以在format.js{}中传递的内容的api,但我找不到..首先:format.js是什么语句,是变量