I tried to implement Ajax processing of like function in Rails

Introduction

I implemented a like function in Rails, but when I used this function, the following problems occurred.

--Since the entire page is re-rendered, it involves unnecessary processing. --Since page transitions are involved, even if you make a like or comment at the bottom of the page, it will transition to the top of the page, reducing usability.

Therefore, I decided to add Ajax function and improve usability without accompanying page transition.

The first implementation method I thought was to use JavaScript / jQuery to fire Ajax processing, but when I investigated, Rails had `` `link_toetc. It was said that Ajax processing can be used by adding remote: true``` to the method of. Unlike the implementation method I thought at first, it was simple, so I immediately implemented it with that method.

Premise

--The posting function and like function have already been implemented. --The like function that is implemented is of the type that involves page transition when the like button is pressed. --For the above, it is assumed that Ajax processing is implemented by asynchronous communication. This article is an introduction to the steps taken in that implementation.

Environment / version

Implementation

First, let `` app / views / posts / show.html.erb``` make the like button app / views / likes / _like.html.erb``` to `render``` ..

erb:app/views/posts/show.html.erb


#···abridgement

<div class="post-like mr-auto">
  <%= render partial: 'likes/like', locals: { post: @post, like: @like } %>
</div>

#···abridgement

Add the Ajax function by adding `remote: true``` to the arguments of the like function delete``` and `` post```.

erb:app/views/likes/_like.html.erb


<% if current_user.already_liked?(post) %>
  <%= link_to 'Like', post_like_path(post, like), method: :delete, class:'post-like__cancel', remote: true %>
<% else %>
  <%= link_to 'Like', post_likes_path(post), method: :post, class:'post-like__enable', remote: true %>
<% end %>

By adding it, you will be able to execute the following processing after clicking each link.

-- delete method → `app / views / likes / destroy.js.erb``` file execution --``` post``` method → Execute the file `app / views / likes / create.js.erb```

Next, the js.erb file is a method of Controller such as `@ post``` or @like``` as shown in the code below. You can use the defined instance variables. this is convenient.

erb:app/views/likes/destroy.js.erb


$('.post-like').html('<%= j(render partial: 'likes/like', locals: { post: @post, like: @like }) %>');

erb:app/views/likes/create.js.erb


$('.post-like').html('<%= j(render partial: 'likes/like', locals: { post: @post, like: @like }) %>');

What was made

The result is as follows. I was able to confirm that it was working without page transitions.

like-ajax.gif

finally

In addition, I was able to implement a follow function and comments. In the future, I will also create a search function, so I will use Ajax as well.

Reference article

Easily implement Ajax (asynchronous communication) with remote: true and js.erb in Rails! (With demo of like function)

Recommended Posts

I tried to implement Ajax processing of like function in Rails
[Rails / JavaScript / Ajax] I tried to create a like function in two ways.
[Rails] I tried to implement "Like function" using rails and js
How to implement a like feature in Ajax in Rails
Implement user follow function in Rails (I use Ajax) ②
Implement user follow function in Rails (I use Ajax) ①
I tried to implement the like function by asynchronous communication
[Swift] I tried to implement the function of the vending machine
[Rails] I tried to implement batch processing with Rake task
I tried to implement the image preview function with Rails / jQuery
I tried to implement polymorphic related in Nogizaka.
I tried to organize the session in Rails
I tried to implement deep learning in Java
How to implement a like feature in Rails
Implement the Like feature in Ajax with Rails.
I tried to implement Firebase push notification in Java
I tried to implement a function equivalent to Felica Lite with HCE-F of Android
Implement follow function in Rails
Implementation of like function (Ajax)
I tried to make a login function in Java
I want to define a function in Rails Console
I tried to implement the Euclidean algorithm in Java
[Rails] Implementation of like function
Rails API mode I tried to implement the keyword multiple search function using arrays and iterative processing.
I tried to make a message function of Rails Tutorial extension (Part 1): Create a model
Rails sorting function implementation (displayed in order of number of like)
I tried to implement a buggy web application in Kotlin
I tried to make a client of RESAS-API in Java
[Rails] Asynchronous implementation of like function
[Rails] About implementation of like function
Implement simple login function in Rails
Implement CSV download function in Rails
Implementation of like function in Java
I tried to make a reply function of Rails Tutorial extension (Part 3): Corrected a misunderstanding of specifications
I tried to develop the cache function of Application Container Cloud Service in the local environment
I tried to make a message function of Rails Tutorial extension (Part 2): Create a screen to display
[Ruby on Rails] Since I finished all Rails Tutorial, I tried to implement an additional "stock function"
[Rails] A simple way to implement a self-introduction function in your profile
I tried to sort the data in descending order, ascending order / Rails
I tried to write code like a type declaration in Ruby
Rails Tutorial Extension: I tried to create an RSS feed function
I tried to make a group function (bulletin board) with Rails
How to implement search functionality in Rails
I tried to introduce CircleCI 2.0 to Rails app
I tried migrating Processing to VS Code
[Rails] Implementation of multi-layer category function using ancestry "I tried to make a window with Bootstrap 3"
I tried to make the "Select File" button of the sample application created in the Rails tutorial cool
I tried to implement the Iterator pattern
Implemented follow function in Rails (Ajax communication)
How to implement ranking functionality in Rails
I tried to use Selenium like JQuery
How to implement asynchronous processing in Outsystems
[Ruby on rails] Implementation of like function
I tried to implement ModanShogi with Kinx
I tried to make a parent class of a value object in Ruby
[Rails] I want to send data of different models in a form
[Rails] I tried to implement a transaction that combines multiple DB processes
[iOS] I tried to make a processing application like Instagram with Swift
I tried to make full use of the CPU core in Ruby
"Teacher, I want to implement a login function in Spring" ① Hello World
[Rails] I tried to raise the Rails version from 5.0 to 5.2