Many programmer uses jquery to catch click event and do $.get or $.post to access the server and print the response in a required div.
But there is a handy way to do ajax request using UJS provided by Rails .
Let us take an example of add to wish list
event.
This can be easily achieved in three easy steps. Now lets see how to do that in rails using UJS
Step 1:
In view(show.haml.html
) your may have
1
|
|
Now we make it as ajax request by adding :remote => true
1
|
|
Step 2:
Now in controller
1 2 3 4 5 6 |
|
Step 3:
Now create a file named wish_list.js.erb
1 2 3 4 5 |
|
Step 4(optional):
You can use jquery to show the loading part while server is responding
1 2 3 |
|
That’s it. As simple as that.