いいね!機能実装におけるNo template found
Q&A
Closed
解決したいこと
現在、いいね!機能の実装をしているのですが、No template foundといいねができないといった状況にあります。
どうしたらいいねができる状態になるのか教えていただきたいです。
発生している問題・エラー
No template found for LikesController#create, rendering head :no_content
Completed 204 No Content in 95ms (ActiveRecord: 14.2ms | Allocations: 27908)
該当するソースコード
destroy.js.erb
$('#like-btn-<%= @way.id %>').html("<%= escape_javascript(render partial: "way", locals: { way: @way }) %>");
create.js.erb
$('#like-btn-<%= @way.id %>').html("<%= escape_javascript(render partial: "way", locals: { way: @way }) %>");
ターミナル
Started POST "/ways/3/add" for ::1 at 2021-01-30 12:00:35 +0900
(0.6ms) SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC
Processing by LikesController#create as JS
Parameters: {"way_id"=>"3"}
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
Way Load (0.2ms) SELECT `ways`.* FROM `ways` WHERE `ways`.`id` = 3 LIMIT 1
↳ app/controllers/likes_controller.rb:20:in `set_like'
CACHE Way Load (0.0ms) SELECT `ways`.* FROM `ways` WHERE `ways`.`id` = 3 LIMIT 1
↳ app/controllers/likes_controller.rb:7:in `create'
TRANSACTION (0.2ms) BEGIN
↳ app/controllers/likes_controller.rb:8:in `create'
User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
↳ app/controllers/likes_controller.rb:8:in `create'
TRANSACTION (0.2ms) ROLLBACK
↳ app/controllers/likes_controller.rb:8:in `create'
No template found for LikesController#create, rendering head :no_content
Completed 204 No Content in 95ms (ActiveRecord: 14.2ms | Allocations: 27908)
class LikesController < ApplicationController
before_action :authenticate_user!
before_action :set_like
def create
user = current_user
way = Way.find(params[:way_id])
like = Like.create(user_id: user.id, way_id: way.id)
end
def destroy
user = current_user
way = Way.find(params[:way_id])
like = Like.find_by(user_id: user.id, way_id: way.id)
like.delete
end
private
def set_like
@way = Way.find(params[:way_id])
end
end
class CreateLikes < ActiveRecord::Migration[6.1]
def change
create_table :likes do |t|
t.references :user, foreign_key: true
t.references :way, foreign_key: true
t.timestamps
end
end
end
Rails.application.routes.draw do
resources :posts
devise_for :users
devise_scope :user do
post 'users/guest_sign_in', to: 'users/sessions#new_guest'
end
root to: "concretes#index"
resources :ways do
post 'add' => 'likes#create'
delete '/add' => 'likes#destroy'
end
resources :questions, only: [:index,:new,:edit,:show]
resources :answers, only: [:index,:new,:edit,:show]
resources :words, only: [:index,:new,:edit,:show]
end
自分で試したこと
・https://qiita.com/soehina/items/a68ab66da3ea1d260301
のサイトを参考にやり直しをしたこと。
・アソシエーションの確認
0