- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我需要重写 Ruby on Rails 中的许多路径辅助方法并从中调用 super。我的标准方法是:
path_helper.rb
def business_path(business)
if params[:city_id] == 2
moscow_business_path(business)
else
super
end
end
但是我有很多这样的方法,所以我想像这样动态地定义它们:
%i[root businesses business ...].each do |path_method|
method_name = "#{path_method}_path"
old_method = method(method_name)
define_method(method_name) do |*args|
if params[:city_id] == 2
public_send("moscow_#{method_name}")
else
old_method.call(*args)
end
end
end
但是我得到这个错误:
/home/leemour/Ruby/burobiz/app/helpers/path_helper.rb:31:in `method': undefined method `root_path' for class `Module' (NameError)
from /home/leemour/Ruby/burobiz/app/helpers/path_helper.rb:31:in `block in <module:PathHelper>'
from /home/leemour/Ruby/burobiz/app/helpers/path_helper.rb:29:in `each'
from /home/leemour/Ruby/burobiz/app/helpers/path_helper.rb:29:in `<module:PathHelper>'
from /home/leemour/Ruby/burobiz/app/helpers/path_helper.rb:1:in `<top (required)>'
from /home/leemour/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/activesupport-5.1.3/lib/active_support/dependencies.rb:476:in `load'
我猜还没有包含辅助模块,所以没有原始路径辅助方法可以用 method(method_name)
捕获。然后我想我必须使用 self.included
钩子(Hook),但我想不出来。我如何调整此代码以使其工作? (我不想使用 eval)。
最佳答案
您是否尝试过使用 Rails.application.routes.url_helpers
方法,而不是尝试使用 Kernel.method
?
%i[...].each do |path_method|
define_method("#{path_method}_path") do |*args|
if params[:city_id] == 2
public_send("moscow_#{path_method}_path", *args)
else
Rails.application.routes.url_helpers.public_send("#{path_method}_path", *arsg)
end
end
end
关于ruby-on-rails - 使用 define_method 覆盖 Rails 中的路径助手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50494898/
有时我需要(为了让我的开发更快)在我的代码中对一些东西进行硬编码。这可能是凭据,或者可能只是一个允许我测试某些功能的 hack。由于很多原因,我从来不想将这段代码推送到主代码库甚至开发分支。一段时间以
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 2 年前。 Improve this qu
我经常发现自己从类中提取常见行为到只包含一组静态方法的帮助程序/实用程序类中。我经常想知道是否应该将这些类声明为抽象类,因为我真的想不出实例化这些类的正当理由? 将这样的类声明为抽象类的优点和缺点是什
如果我这样做: $obj = factory(Object::class)->make(); collect($obj); 我返回了一个类型的集合: Illuminate\Support\Collec
我有一个应用程序,我可以在其中列出 parent 和 child 。当我添加一个 child 时,我需要获得一个 parent 列表作为下拉列表显示。有没有类似 collection_select 的
我有渲染组件( source ),用于从 Controller 字段渲染组件/助手。它适用于 ember 1.9.1,但在更新到 ember 1.12.1 后,我发现 API 发生了变化。之后upda
我刚开始使用 Rails,还有很多东西需要学习,所以我可能会比平常更频繁地在 Stackoverflow 上询问初学者 Rails/Ruby 问题。 我只是想弄清楚 Helpers 在 Rails 中
我在 Web 上使用 VS Express 2012。我的项目是一个 MVC 4 项目。 我正在尝试创建一个助手来创建一个菜单项,该菜单项是带有标签的 png 图像。我几乎剪切并粘贴了这段代码: Ac
我正在尝试在我的 Vue 页面中映射我商店的状态变量: export default { data: () => ({ localData: []
我目前正在开发一个 Rails 插件,用于生成 iPhone 特定的 HTML 元标记。我尝试使用 ActionView::TestCase 进行单元测试,但不断收到相同的错误。请参阅下面的文件内容和
我正在努力解决一个与变量声明相关的非常基本的问题。我已经阅读了有关变量的所有内容,但我不知道我的问题是否与 1) 我如何声明变量或 2) 我如何设置变量的范围有关。 首先,我对 Meteor 中变量的
我想知道是否可以将参数传递给 Meteor Helper 并在 HTML 中插入返回对象的属性,而不仅仅是返回最终值。我有这样的东西: HTML: {{#each conversation}}
我正在尝试为我的 Ember 应用程序构建一个新的条件助手。值得一提的是,我正在使用使用 Handlebars 2.0 的 Ember 1.10.1,并且我无法升级它,如果能很好地解决这个版本的 Em
我最近从 Dreamweaver 迁移到 aptana,并尝试使 aptana 尽可能相似;) 已经做了很多更改,但我仍然找不到使代码字体变小的方法(在 Dreamweaver 中代码更清晰,因为字体
我有以下模板: {{#each helperOne "1" "2" }} Lorem upsom {{#each}} helper : template.tempName.h
我有这个简单的代码块,它位于我的一个模板上,但我想将其作为助手放置,以便我的所有 View 都可以访问它。 @hidden(field: Field) = { @defining(field)
使用正则表达式,我正在替换 **text in bold**至 text in bold在一个字符串中,然后显示 message使用 {{{message}}}在我的 EmberJS 模板上。问题是我
是否有任何非常有用和健壮的 C++ 网络库?和库来帮助他们更好地运行?诸如使用 << 时自动进行字节序转换之类的东西,阻止读取直到结构或 w/e 您的读取完全传输,有助于调试协议(protocol)的
模板 {{#each tags}} {{#isObject this}} Object {{else}}
我有返回 JSON 的函数: Template.mainmenu.menuitem = function() { var jsonObj = { items: [ { ur
我是一名优秀的程序员,十分优秀!