草庐IT

route_param

全部标签

ruby - pg: exec_params 不替换参数?

第一次使用pggem访问postgres数据库。我已成功连接并可以使用#exec运行查询,但现在使用#exec_params构建一个简单的查询似乎没有替换参数。即:get'/databases/:db/tables/:table'do|db_name,table_name|conn=connect(db_name)query_result=conn.exec_params("SELECT*FROM$1;",[table_name])end结果为#这似乎是一个非常简单的示例-我是否从根本上误解了如何使用此方法? 最佳答案 您可以将占位

ruby-on-rails - 我可以在没有 Controller 的情况下直接从 routes.rb 渲染布局吗?

我想为网站的管理和公共(public)部分设置一对样式指南。每个都需要自己的布局,其中包含静态html和调用erbpartials的混合(因此静态页面不会削减它)。我不需要Controller来为这些页面提供服务,而且我不希望有效的仅开发内容使其余代码困惑。这让我想知道是否有一种方法可以直接呈现布局。免责声明:我明白这不是我应该经常/永远做的事情,而且我知道有很多争论可以解释为什么这是一个坏主意。我对这是否可能感兴趣。有没有办法让我直接从routes.rb渲染布局而不通过Controller? 最佳答案 出于某种奇怪的原因,我想暂时

ruby-on-rails - rails : route helpers for nested resources

我有如下嵌套资源:resources:categoriesdoresources:productsend根据RailsGuides,Youcanalsouseurl_forwithasetofobjects,andRailswillautomaticallydeterminewhichrouteyouwant:Inthiscase,Railswillseethat@magazineisaMagazineand@adisanAdandwillthereforeusethemagazine_ad_pathhelper.Inhelperslikelink_to,youcanspecifyju

ruby - 无法理解 Grape API 路由参数

我在理解GrapeAPI时遇到很多困难,特别是route_param以及它如何仅使用params。考虑这段代码:desc"Returnastatus."paramsdorequires:id,type:Integer,desc:"Statusid."endroute_param:iddogetdoStatus.find(param[:id])endend这个街区产生什么路线?我知道这是一个get请求,但为什么它被包裹在route_paramblock中?为什么它不能在paramsblock中? 最佳答案 你的block产生这条路线:

ruby-on-rails - routes.rb - 不支持 Controller 名称

我遇到了一个很奇怪的问题。这是与routes.rb相关的部分:resources:playersmatch'/players/:userid',:to=>'Players#show'当您访问localhost:3000/players/1234时出现此错误:'Players'isnotasupportedcontrollername.Thiscanleadtopotentialroutingproblems.Controller中的相关代码:defshowbeginifPlayer.find_by(:uid=>:userid)then@playerattributes=Player.f

ruby-on-rails - Rails 5 Rspec 接收 ActionController::Params

我刚刚升级到Rails5。在我的规范中,我有以下内容期望(模型).toreceive(:update).with(foo:'bar')但是,由于params不再扩展Hash而现在是ActionController::Parameters规范失败了,因为with()期待一个散列,但它实际上是ActionController::Parameters是否有更好的方法在Rspec中做同样的事情,例如不同的方法with_hash?我可以使用解决这个问题expect(model).toreceive(:update).with(hash_including(foo:'bar'))但这只是检查参数是

ruby-on-rails - 未初始化的常量 ActionDispatch::Routing::Routes (NameError)

我正在学习clickatell教程,我的代码如下所示。但是我得到了错误uninitializedconstantActionDispatch::Routing::Routes(NameError)from/Library/Ruby/Gems/1.8/gems/actionpack-3.1.1/lib/action_dispatch/routing/route_set.rb:258:in`instance_exec'from/Library/Ruby/Gems/1.8/gems/actionpack-3.1.1/lib/action_dispatch/routing/route_set.

ruby-on-rails - 我可以重命名 routes.rb 中的资源吗?

在routes.rb中,我目前有resources:users用于UserController。访问者可以通过www.mydomain.com/users请求User模型我想保持UserController不变,但改为请求people的URL,以便访问者看到以下URL:www.mydomain.com/people对于单个请求,我可以通过以下方式执行此操作:get'/users',to:'users#index'对于资源map是否也可能如此? 最佳答案 您可以使用resources的path选项方法:resources:users,

ruby-on-rails - 葡萄 : required params with grape-entity

我正在用grape编写一个API服务器,我选择使用grape-entity因为它能够自动生成swagger的文档。但是现在我在按要求设置参数时遇到了问题。因为葡萄不验证参数是否存在。看起来grape忽略了实体参数的required:true。app.rbmoduleSmartmoduleVersion1classAppos_entity.rbmoduleSmartmoduleEntitiesclassOSEntityapp_entity.rbmoduleSmartmoduleEntitiesclassAppEntity现在其他一切都很好,但我不知道如何以DRY方式使用实体,并让grap

ruby - 如何在 Ruby 中检查参数 "param[:some_value]"

我知道一些检查参数是否为零的方法ifparam[:some_value]ifparam[:some_value].present?if!param[:some_value].nil?#unlessparam[:some_value].nil?if!param[:some_value].blank?#unlessparam[:some_value].blank?哪一个是正确的并且最受欢迎?它们之间有什么区别?我宁愿使用ifparam[:some_value]因为它最简单也最短。 最佳答案 以下是nil?、blank?和present?