Ruby on Rails5でいいねボタンをAjaxで実装しようとしてるのですが、うまく動作しません。
いいねを取り消してからいいねをおす時にエラーが発生します。
発生してるエラーはルーティングエラーで、No route matches [GET]
と出てきます。
フォームには、method: :post
と、指定しているのですが、なぜかこのエラーが出てしまいます。
remote: true
を消して、Ajaxを無効にしたら、正常に動作することは確認しています。
posts/show.html.erb
<li class="activity-list__item" id="like_form"> <%= render'likes/like_form' %> </li>
likes/_like_form.html.erb
<% if user_signed_in? %> <% if current_user.already_liked?(@post) %> <span> <%= link_to(post_like_path(@post), method: :delete, remote: true) do %> <%= image_tag 'gooded.svg', class: "good-icon" %> <% end %> <p class="arrow_box">いいねを外す</p> </span> <%= @post.likes.count %> <% else %> <span> <%= link_to(post_likes_path(@post), method: :post, remote: true) do %> <%= image_tag 'good.svg', class: "good-icon" %> <% end %> <p class="arrow_box">いいね!</p> </span> <%= @post.likes.count %> <% end %> <% else %> <span> <%= image_tag 'gooded.svg', class: "good-icon" %> <p class="arrow_box"><%= link_to "いいねするにはログインが必要です。", user_session_path, class: "arrow_box__link" %></p> </span> <% end %>
likes_controller.rb
class LikesController < ApplicationController before_action :authenticate_user! def create @like = current_user.likes.create(post_id: params[:post_id]) @post = Post.find(params[:post_id]) end def destroy @like = Like.find_by(post_id: params[:post_id], user_id: current_user.id) @post = Post.find(params[:post_id]) @like.destroy end def index @user = User.find(params[:user_id]) likes = Like.where(user_id: @user) @posts = [] likes.each do |like| @posts << Post.find_by(id: like.post_id) end end end
lises/create.js.erb
js
1$('#like_form').html("<%= j(render partial: 'likes/like_form') %>"); 2
likes/destroy.js.erb
js
1$('#like_form').html("<%= j(render partial: 'likes/like_form') %>"); 2
routes.rb
Rails.application.routes.draw do root to: 'home#index' devise_for :users resources :users, only: %i[index show] do resources :likes, only: %i[index] member do get :following, :followers end end resources :relationships, only: %i[create destroy] resources :posts, only: %i[index new show create destroy search] do resources :likes, only:%i[create destroy] resources :comments, only: %i[create edit] end end
$rails routes
の結果
root GET / home#index new_user_session GET /users/sign_in(.:format) devise/sessions#new user_session POST /users/sign_in(.:format) devise/sessions#create destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy new_user_password GET /users/password/new(.:format) devise/passwords#new edit_user_password GET /users/password/edit(.:format) devise/passwords#edit user_password PATCH /users/password(.:format) devise/passwords#update PUT /users/password(.:format) devise/passwords#update POST /users/password(.:format) devise/passwords#create cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel new_user_registration GET /users/sign_up(.:format) devise/registrations#new edit_user_registration GET /users/edit(.:format) devise/registrations#edit user_registration PATCH /users(.:format) devise/registrations#update PUT /users(.:format) devise/registrations#update DELETE /users(.:format) devise/registrations#destroy POST /users(.:format) devise/registrations#create user_likes GET /users/:user_id/likes(.:format) likes#index following_user GET /users/:id/following(.:format) users#following followers_user GET /users/:id/followers(.:format) users#followers users GET /users(.:format) users#index user GET /users/:id(.:format) users#show relationships POST /relationships(.:format) relationships#create relationship DELETE /relationships/:id(.:format) relationships#destroy post_likes POST /posts/:post_id/likes(.:format) likes#create post_like DELETE /posts/:post_id/likes/:id(.:format) likes#destroy post_comments POST /posts/:post_id/comments(.:format) comments#create edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) comments#edit posts GET /posts(.:format) posts#index POST /posts(.:format) posts#create new_post GET /posts/new(.:format) posts#new post GET /posts/:id(.:format) posts#show DELETE /posts/:id(.:format) posts#destroy rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
エラーの全文は以下のとうりです。
ActionView::Template::Error (No route matches {:action=>"destroy", :controller=>"likes", :post_id=>#<Post id: 23, user_id: 1, created_at: "2019-10-01 01:51:32", updated_at: "2019-10-01 01:51:32", image: nil, picture: nil, title: "タイトル", pictures: ["good.png"], tag_list: nil>}, missing required keys: [:id]):
エラーは Ajaxでの更新時のみに発生して、画面を更新すると、ボタンを押した結果も更新されていて、正常に表示されます。
回答2件
あなたの回答
tips
プレビュー