Accessing rails routes in javascript

后端 未结 8 1610
星月不相逢
星月不相逢 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:37

    kishore I think this is the simpliest way, just call:

    Rails.application.routes.url_helpers.*_path

    So, if you have in your routes.rb, let's say:

    resources :users

    then you want to call the index action from a javascript file, you do:

    $.get('<%= Rails.application.routes.url_helpers.users_path %>', function(data){ ...
    

    Take into account that the js file should have a .erb extension (or *.js.erb) so rails knows that must be preprocessed. Otherwise, the file will be served as is.

    0 讨论(0)
  • 2020-12-08 20:38

    I was researching this question for a while and didn't found any implementation compatible with Rails 3.

    So decided to write my own:

    https://github.com/railsware/js-routes

    0 讨论(0)
提交回复
热议问题