如何在Rails 3中定义自己的路由助手? [英] How to define own routing helpers in rails 3?

查看:93
本文介绍了如何在Rails 3中定义自己的路由助手?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use polimorphic_path and it some buggy. This method require some route helper that not defined. How i can define (like regular method) own route helper wich will be used like "model_name_path, model_name_url etc"?

推荐答案

此解决方案对我有用。

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

Add this code to the end of config/routes.rb file. Make sure to replace MyApp with your application's name.

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

这些自定义方法将在控制器,视图和测试中可用。

These custom methods will be available in controllers, views and tests.

这篇关于如何在Rails 3中定义自己的路由助手?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆