dimanche 27 décembre 2015

Ruby on Rails, writing custom AJAX call in the background


I am working on my first Ruby on rails app and am facing some troubles with using AJAX in the Rail's type to do it.

I have the following resources:

  • WhiteLists [ID, various_data]
  • BlackLists [ID, various_data]
  • Users [ID, Name, email, role, ...]
  • UserListsExpiry [User_id, WhiteList_id, BlackList_id, valid_from, valid_to]

And a custom view, which mainly works with javascript and should load existing and later on save both resources (for a specific User_id within valid_from and valid_to in the join-Table) via AJAX.

So in the mentioned view, the user can customize the data, which has the form of JSON.stringified Arrays of Objects which I want to store in the database. (Currently only saved in the local BrowserStorage). One array (many different single dataset-entries/queries) needs to be saved into the WhiteList and the other in the Blacklist model creating a new entry within the join Table UserListsExpiry (valid_from and valid_to is seperately submitted with the 2 arrays).

How do I make the Arrays accessible in the controller out of a view (passing via AJAX I guess), and which functions/routes do I have to set to be able to load a whole dataset (e.g. all White- and Blacklist-entries for a specific User_id where the current date is between valid_from and valid_to and build again a JSON.stringified Array out of it)?

All tutorials I found were treating how to write AJAX Calls within forms and the standard views, but not how to pass a custom dataset and handle this specifically.

I'd really appreciate some assistance! Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire