How to dynamically call routes helper in rails?

前端 未结 2 1401
盖世英雄少女心
盖世英雄少女心 2021-02-03 21:08

For example, I have constructed a string called \"new_work_path\", now I want to call that helper as a method.

I\'ve tried send(\"new_work_path\", vars) and

2条回答
  •  囚心锁ツ
    2021-02-03 21:41

    try Rails.application.routes.url_helpers.send(...)

    Edit:

    As Larry Gebhardt mentioned the url_helpers module is no longer being cached.

    Another workaround would be:

    cached_helpers = Class.new do
      include Rails.application.routes.url_helpers
      include Rails.application.routes.mounted_helpers
    end.new
    
    cached_helpers.send(...)
    

提交回复
热议问题