Accessing rails routes in javascript

后端 未结 8 1690
星月不相逢
星月不相逢 2020-12-08 19:39

Can anyone explain how i can access the rails routes/names routes in javascript ?

The following are some of the things i tried http://github.com/jsierles/js_named_r

8条回答
  •  没有蜡笔的小新
    2020-12-08 20:26

    1. gem install the "angular_rails_templates"
    2. Create a file called angular_rails_templates.rb in the config/initializers folder
    3. copy following code in the file and restart server. (including "module CustomERBEngine" for it cannot be added to code block by 4 space)

    module CustomERBEngine

    class ERBTemplate < Tilt::ERBTemplate
    
    def evaluate(scope, locals, &block)
      scope.class_eval do
        include Rails.application.routes.url_helpers
        include Rails.application.routes.mounted_helpers
        include ActionView::Helpers
       end
          super
        end
      end
    end
    
    Tilt.register CustomERBEngine::ERBTemplate, '.erb'
    

提交回复
热议问题