0

I have a normal rails app website, but on some pages I need to use AJAX.

I already have some working AJAX Javascript code using jQuery, but so far I haven't used any rails helper to do that, writing strings corresponding to paths manually.

But is there a more convenient way to do it in javascript ? Suppose I have a javascript function which takes an ID as argument, and must call an AJAX action. So far I've been doing it this way

var url = "/tags/tagID"
function getTag(tag_id){
    $.get(url.replace("tagID", tag_id) +'.json')
        .fail(function(data){
            alert('Oops error !');
        })
        .success(function( data ) {blabla ] )
}

Is it possible to rename the .js to .js.erb and use path helpers ? So I could get rid of this url variable and write

routes.rb

resources :tags

tags.js.erb

$.get(tag_path("tagID").replace("tagID", tag_id)....

Or is there a more convenient way to do this ? I only need very little AJAX, so I don't want to use a frontend framework (Angular, etc.), just jQuery

EDIT My scenario

A user searches for a given tag thanks to an autocomplete searchbar. This searchbar will return the ID somehow.

The user can select several tags this way, and their IDs will be stored in an array. Now, upon clicking a button, I want to send a query to a non-RESTful (with the ID array as parameter) controller action via AJAX. For now I will focus on sending one item at a time (so just one ID string), for it is easier/more reactive.

This action is actually going to look in my models for projects and ingeneers that possess this tag, and return a JSON with formatted results.

Cyril Duchon-Doris
  • 12,964
  • 9
  • 77
  • 164
  • From where is the `getTag` function called? Why not pass the path as argument instead of just the id? – AbM Jul 06 '15 at 12:55
  • Hmm yeah that's an idea, but then the code I will put inside this function will only work for a given route/type of object (which actually isn't going to be RESTful at all ^^"). And actually, the tag_id itself is coming from an autocompletion searchbar, so I only have the ID. – Cyril Duchon-Doris Jul 06 '15 at 13:04
  • I added a small user scenario so maybe it's easier to understand my goal/constraints. – Cyril Duchon-Doris Jul 06 '15 at 13:12

1 Answers1

1

Yes, you can use *.js.erb to use Rails helpers. Rails provides some handy helpers to work with Ajax. Normally with rails you can use them by using the the tag remote: true.

In your case something like

<%= link_to 'Tags', tags_path(<tag.id>), remote: true %> (roughly),

Read more about using Rails helpers with Ajax here, and this explains it nicely.

Update

Rails is using CSRF token to validate requests (except GET), so if you are going to use pure HTML/JavaScript, you want to add the token to your request. Have a look at this post on the same.

I agree there is no out-of-the-box way of doing that, but there are few workarounds.

halfer
  • 19,824
  • 17
  • 99
  • 186
sameera207
  • 16,547
  • 19
  • 87
  • 152
  • Yes ok, but this is writing to the HTML directly. In my case, a series of actions will fire some javascript, and I cannot really code it with a `<%= link_to %>`, the javascript must do some AJAX directly – Cyril Duchon-Doris Jul 06 '15 at 13:06
  • And no, rails path helpers do not work directly in .js.erb, see [this SO question](http://stackoverflow.com/questions/6725629/route-helpers-in-asset-pipeline) – Cyril Duchon-Doris Jul 06 '15 at 21:31
  • Thanks for the update. Interesting stuff in your link. I also knew about CSRF, but in my case I just need GET requests so it should be fine. – Cyril Duchon-Doris Jul 13 '15 at 16:03