代码之家  ›  专栏  ›  技术社区  ›  D9SeveN

如何在Rails5中的类似/不同按钮上使用RailsAjax

  •  0
  • D9SeveN  · 技术社区  · 7 年前

    我已经完成了喜欢/不喜欢的功能。但是我想在这个函数上使用ajax。我下一步该怎么办?

     def new
       @like = Like.new like_params
     end
    
     def create
       @user = current_user.id
       @comment = params[:comment_id]
       like_params = {user_id: @user, comment_id: @comment}
    
       @like = Like.new like_params
       if @like.save
         redirect_to song_path @song
       else
         render "new"
       end
     end
    
     def destroy
       @like.destroy
       redirect_to song_path @song
     end
    

    结束

    和视图:songs/show.html.erb

      <h3>Comments:</h3>
      <%= render "comments/comment" %>
    

        <% if user_signed_in? && current_user.likes?(comment) %>
          <% like = comment.likes.find_by comment_id: comment.id, user_id: current_user.id %>
          <%= link_to song_comment_like_path(@song, comment, like), method: :delete do %>
            Unlike
          <% end %>
        <% else %>
          <%= link_to song_comment_likes_path(@song, comment), method: :post do %>
            Like
          <% end %>
        <% end %>
    

    我的路由器:

    resources :songs do
      resources :comments do
        resources :likes
      end
    end
    
    0 回复  |  直到 7 年前