gpt4 book ai didi

ruby-on-rails - 如何在rails 3中定义自己的路由助手?

转载 作者:行者123 更新时间:2023-12-03 16:18:41 25 4
gpt4 key购买 nike

我使用 polimorphic_path 并且它有一些错误。此方法需要一些未定义的路由助手。我如何定义(像常规方法一样)自己的路由助手,它将像“model_name_path、model_name_url 等”一样使用?

最佳答案

这个解决方案对我有用。

将此代码添加到 config/routes.rb 的末尾文件。确保更换 MyApp使用您的应用程序名称。

MyApp::Application.routes.named_routes.module.module_eval do
def model_name_path(*args)
# Your code here
end

def model_name_url(*args)
# Your code here
end
end

MyApp::Application.routes.named_routes.instance_eval do
@helpers += [:model_name_path, :model_name_url]
end

这些自定义方法将在 Controller 、 View 和测试中可用。

关于ruby-on-rails - 如何在rails 3中定义自己的路由助手?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6470656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com