投稿に対していいねする機能を実装しているのですが、非同期化をしている際にエラーが発生し、解決できない状況です。
よろしくお願いします。
consoleError
1jquery-3.1.1.min.js:4 2DELETE http://localhost:3000/odais/24/copies/33/likes/42 422 (Unprocessable Entity)
applicationHtmlHaml
1!!! 2%html 3 %head 4 %meta{content: "text/html; charset=UTF-8", "http-equiv": "Content-Type"}/ 5 -# bootstrapのmeta読み込み 6 %meta{content: "width=device-width, initial-scale=1, shrink-to-fit=no", name: "viewport"}/ 7 %title CopyWritter 8 -# bootstrapのcss読み込み 9 %link{crossorigin: "anonymous", href: "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css", integrity: "sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T", rel: "stylesheet"}/ 10 %link{href: "https://use.fontawesome.com/releases/v5.6.1/css/all.css", rel: "stylesheet"}/ 11 %link{href: "css/style.scss", rel: "stylesheet"}/ 12 %script{src: "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"} 13 14 = csrf_meta_tags 15 = csp_meta_tag 16 = stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' 17 = javascript_include_tag 'application', 'data-turbolinks-track': 'reload' 18 19 %body 20 21 -#~省略~ 22 23 = yield 24 25 -# bootstrapのjs読み込み 26 %script{crossorigin: "anonymous", integrity: "sha384-3ceskX3iaEnIogmQchP8opvBy3Mi7Ce34nWjpBIwVTHfGYWQS9jwHDVRnpKKHJg7", src: "https://code.jquery.com/jquery-3.1.1.min.js"} 27 %script{crossorigin: "anonymous", integrity: "sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1", src: "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"} 28 %script{crossorigin: "anonymous", integrity: "sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM", src: "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"} 29 30 %footer 31 -#~省略~ 32
likeJs
1$(function(){ 2 3 function likeIcon(){ 4 var html = `<div class="copies__a__copy__like__icon" > 5 <a rel="nofollow" data-method="post" href="/odais/${odai.id}/copies/${copy.id}/likes"> 6 <i class= "far fa-heart"> 7 ::bofore 8 </i> 9 </a> 10 </div> 11 ` 12 return html; 13 } 14 15 $('.copies__a__copy__liked__icon').on('click',function(){ 16 var url = $(this).find('a').attr('href'); 17 var liked_id = $(this).data('id'); 18 // console.log(url) 19 // console.log(liked_id) 20 $.ajax({ 21 type: 'DELETE', 22 url: url, 23 dataType: 'json', 24 data: {id: liked_id} 25 }) 26 .done(function(){ 27 $(this).remove(); 28 var html = likeIcon(); 29 $('.copies__a__copy__name').append(html); 30 }) 31 // .fail(function(){ 32 // alert('error'); 33 // }) 34 }); 35 36 37})
indexHtmlHaml
1-# 分類 2.classification 3 4-#〜省略〜 5 6.contents 7 .copies 8 - @copies.each do |copy| 9 .copies__a__copy.col-md-6 10 11 -# 部分テンプレート候補 12 .copies__a__copy__name 13 = copy.user.name 14 15 -# いいね部分テンプレート user#showのいいねとは別ファイル 16 -# Likesコントローラーでeachを使うと一つ一つのコピーと紐つけられないので、こちらでデータ取得 17 = render partial: 'like', locals: { copy: copy } 18 19 20 = image_tag copy.odai.image.url, class:'odai_image',width: "250",height:"250" if copy.odai.image.present? 21 .copies__a__copy__text 22 = copy.odai.text 23 .copies__a__copy__catchphrase 24 = copy.catchphrase 25 .copies__a__copy__presentation 26 = copy.presentation 27 28 - if user_signed_in? 29 = link_to "/odais/#{copy.odai.id}/copies/#{copy.id}" ,method: :get do 30 %i.fas.fa-newspaper.fa-2x{"aria-hidden": "true"} 31 32 - if user_signed_in? && copy.user_id == current_user.id 33 34 = link_to "/odais/#{copy.odai.id}/copies/#{copy.id}/edit" ,method: :get do 35 %i.fas.fa-edit.fa-2x{"aria-hidden": "true"} 36 = link_to "/odais/#{copy.odai.id}/copies/#{copy.id}" ,method: :delete do 37 %i.fas.fa-trash-alt.fa-2x{"aria-hidden": "true"} 38
routesRb
1Rails.application.routes.draw do 2 devise_for :users 3 # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 4 root 'copies#index' 5 resources :users 6 resources :odais , only: [:index, :new, :create, :edit, :update, :destroy]do 7 resources :copies do 8 resources :comments , only: [:create, :destroy] 9 resources :likes, only: [:create, :destroy] 10 end 11 end 12end
likesControllerRb
1class LikesController < ApplicationController 2 3 def destroy 4 @like = Like.find_by(params[:id]) 5 copy_id = @like.copy_id 6 @copy = Copy.find_by(id: copy_id) 7 odai_id = @copy.odai_id 8 @odai = Odai.find_by(id: odai_id) 9 @like.destroy if @like.user_id == current_user.id 10 11 respond_to do |format| 12 format.html 13 format.json 14 end 15 16 end 17 18end
destroyJsonJbuilder
1json.array! @copy,@odai do |message| 2 json.copy_id @copy.id 3 json.odai_id @odai.id 4end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/06 09:43
2019/06/06 11:19 編集