简体   繁体   中英

Ruby on Rails on rendering with Ajax

I am working on a simple website which has posts in forms of songs, now I have implemented like and dislike feature, but when I click on like/dislike it renders the numbers of likes/dislikes on all posts. And when I reload the page it returns them to normal. Now I would like it to change only the numbers for that particular post, without affecting the numbers on other posts?

My view for posts:

<div class="col-6">
    <% for @s in @songs %>
    <div class="card border-secondary mb-1">
        <div class="card-header">
            <div class="col-1">
                <%= image_tag User.find(@s.user_id).user_image.url, :size=>"50x50" %>
            </div>
            <div class="col-11 text-muted">
                <a href="/user/<%= User.find(@s.user_id).username %>" class="info-col">
                    <%= User.find(@s.user_id).username %>                           
                </a> 
                - <%= @s.created_at.to_formatted_s(:short) %>
            </div>
        </div>
        <div class="card-body">
        <h4 class="card-title">
            <a href="<%= song_path(@s) %>" class="link-col"><%= @s.title %></a>
        </h4>
        <p class="card-text"><%= simple_format(@s.content) %></p>
        </div>

        <div class="card-footer text-muted">
            <div class="col-12">
                <div class="row">

                    <div class="col-3" id="song_like">
                        <%= render '/components/song_like' %>
                    </div>

                    <div class="col-3" id="song_dislike">
                        <%= render '/components/song_dislike' %>
                    </div>

                    <div class="col-4" id="song_comment">
                        <%= render '/components/song_comment' %>
                    </div>

                </div>
            </div>
        </div>

    </div>
    <% end %>
</div>

The song_like partial has the following code:

    <%= link_to like_song_path(@s.id, like: true), remote: true, method: :post do %>
    <i class="fa fa-thumbs-o-up fa-lg" aria-hidden="true"></i> &nbsp <span class="songlikes">
        <%= @s.thumbs_up_total %>
    </span>        
<% end %>

The song_dislike partial has the following code:

<%= link_to like_song_path(@s.id, like: false), method: :post, remote: true do %>
    <i class="fa fa-thumbs-o-down fa-lg" aria-hidden="true"></i> &nbsp <span class="songdislikes">
        <%= @s.thumbs_down_total %>
    </span>  
<% end %>

And my 'like' controller is like:

def like
@s = Song.find(params[:id])
@like = Like.create(like: params[:like], user: current_user, song: @s)

respond_to do |format| 
if @like.valid?
    format.html
    format.js
else
    format.html
    format.js
  end
end

end

This is how like.js.erb looks like:

    $('.songlikes').html("<%=  @s.thumbs_up_total %>");

$('.songdislikes').html("<%=  @s.thumbs_down_total %>");

Here is the part of routes.rb file:

resources :songs do
  member do
    post 'like'
  end
end

I am assuming there is some issue on rendering or jquery, but can't figure it out. Do You have any instructions?

EDIT: You should remove the redirect_to in your f.html?

This reloads the page - and the js will therefore not work.

An Ajax call is per definition not reloading the page.

If you remove those lines and try the following:


There's no new data in your jQuery.

You need a global attribute, so the view can find the data from your controller.

For example:

@like = ...

Then in your view you can add a local to be rendered:

$("#song_like").load("<%=j render partial: '/components/song_like', locals: {like: @like} %>");

But , you need to change your partial to be a partial. And change the variable to like so it will be rendered in the right way.

Hope it helps!

UPDATE

If you want to hit a specific song you can render it with each and then assign an id to your post's class

For example:

<% @posts.each do |post| %>
  <div class="post-content">
    ... other stuff ...
    <div class="likes like-post-<%= post.id %>">
       .. number of likes
    </div> 
    <%= link_to "Like", link_route_path(post) %>
  </div>
<% end %>


# controller 
# So you can use the id in your js view

@id = params[:id]   

# view.js
$(".like-post-" + "<%= @id %>").html("<%=  @s.thumbs_up_total %>"); 

Hope the idea helps.

change rendering like this:

<div class="col-3" id="song_like">
 <%= render partial: '/components/song_like', locals: {s: @s, like: true} %>
</div>

then in partial:

<%= link_to like_song_path(s.id, like: like), method: :post, remote: true do %>
    <i class="fa fa-thumbs-o-up fa-lg" aria-hidden="true"></i> &nbsp <%= s.thumbs_up_total %>        
<% end %>

and in controller of like_song

should define

@s= Song.find(params[:id])
like = Like.create(like: params[:like], user: current_user, song: @song)
@like = !like.like # opposite to your current status

rest of the things good and then in like.js.erb

$('#songs_like').html("<%= escape_javascript(render partial: '/components/song_like', locals: { s: @s, like: @like } ) %>")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM