
現在、下記ページを下にいいねボタンの政策を行なっておりますが、手順通りに作成しましたが、エラーが出ました。
https://note.com/become_engineer/n/n45f7285622e3
エラー内容としては、見た目上、ボタンはできていて(ハート)、マウスオーバーで手のマークにもなってクリックもできるのですがいるのですが、数字が加算されません。おそらくデータに保存できていないのだと思います。
検証ツールから、consoleを見たところ、
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
のエラーが出てまして、
さらにNetworkを調べてみると、下記のようなエラーでした。
controlllerのクリエイト部分がおかしいのかとも思いますが、訂正方法がわかりません。ご教授いただけると助かります。
(必要そうなコードを全て載せましたが、基本的には上のページ通り作業をし、model,controllerがpostであるものをdogrunに変更しているだけです)
Network
1NoMethodError in LikesController#create 2 3undefined method `dogrun_params' for #<LikesController:0x007ff7dc698b70> 4Did you mean? dogrun_path 5Extracted source (around line #427): 6 7#425 lambda do |target, value, &block| 8#426 target, block, method, *arguments = expand(target, value, block) 9*427 target.send(method, *arguments, &block) 10#428 end 11#429 end 12#430 13 14Extracted source (around line #198): 15 16#196 17#197 unless halted 18*198 result_lambda = -> { user_callback.call target, value } 19#199 env.halted = halted_lambda.call(target, result_lambda) 20#200 if env.halted 21#201 target.send :halted_callback_hook, filter, name 22 23Extracted source (around line #34): 24 25#32 included do 26#33 define_callbacks :process_action, 27*34 terminator: ->(controller, result_lambda) { result_lambda.call; controller.performed? }, 28#35 skip_after_callbacks_if_terminated: true 29#36 end 30#37 31 32中略 33 34 35 36Request parameters 37{"id"=>"1"} 38 39Session dump 40_csrf_token: "eD5G78_KSR3Pgu773OIrX78-mE0TChgn7cdG4K3DmK8=" 41session_id: "d7df6289b1886fb5a8eb177e74b32a12" 42warden.user.user.key: [[1], "$2a$12$xmubuXJKYSU30ylzDnJeV."] 43 44Env dump 45GATEWAY_INTERFACE: "CGI/1.2" 46HTTP_ACCEPT: "text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01" 47HTTP_ACCEPT_ENCODING: "gzip, deflate, br" 48HTTP_ACCEPT_LANGUAGE: "ja,en-US;q=0.9,en;q=0.8" 49HTTP_ORIGIN: "http://localhost:3000" 50HTTP_VERSION: "HTTP/1.1" 51HTTP_X_CSRF_TOKEN: "z1Vn3Hf26pZbHP2MfbpvbwLvHIotke0Y8J1rjlBkDotRXmlu00CawbkhXXwKIgcsDBrr6I5ALB4IZ4W4FFPAzw" 52ORIGINAL_SCRIPT_NAME: "" 53REMOTE_ADDR: "::1" 54SERVER_NAME: "localhost" 55SERVER_PROTOCOL: "HTTP/1.1" 56 57Response headers 58None 59
class
1class LikesController < ApplicationController 2 before_action :dogrun_params 3 def create 4 Like.create(user_id: current_user.id, dogrun_id: params[:id]) 5 end 6 7 def destroy 8 Like.find_by(user_id: current_user.id, dogrun_id: params[:id]).destroy 9 end 10 11 private 12 13 def post_params 14 @dogrun = Dogrun.find(params[:id]) 15 end 16end 17
app/models/user.rb
1class User < ApplicationRecord 2 has_one_attached :image 3 has_many :dogruns, dependent: :delete_all 4 has_many :comments, dependent: :delete_all 5 has_many :likes 6 7 devise :database_authenticatable, :registerable, 8 :recoverable, :rememberable, :validatable 9 validates :name, presence: true 10 11 def liked_by?(dogrun_id) 12 likes.where(dogrun_id: dogrun_id).exists? 13 end 14end 15
dogruns_controller
1class DogrunsController < ApplicationController 2 layout 'dogrun' 3 before_action :set_q 4 before_action :authenticate_user! 5 6 # topページ 7 def top; end 8 9 # 一覧ページ 10 def index 11 @dogruns = Dogrun.order(id: "DESC").last(5) 12 @comments = Comment.order(id: "DESC").last(10) 13 end 14 15 # 検索ページ 16 def search 17 @results = @q.result 18 end 19 20 # GET /dogruns/1 or /dogruns/1.json 21 def show 22 @dogrun = Dogrun.find(params[:id]) 23 @comment = Comment.new # 新規コメント投稿 24 @comments = @dogrun.comments.order(id: "DESC") 25 end 26 27 # GET /dogruns/new 28 def new 29 @dogrun = Dogrun.new 30 end 31 32 # GET /dogruns/1/edit 33 def edit 34 @dogrun = Dogrun.find(params[:id]) 35 end 36 37 # POST /dogruns or /dogruns.json 38 def create 39 @dogrun = Dogrun.new(dogrun_params) 40 @dogrun.user = current_user 41 if @dogrun.save 42 redirect_to dogrun_url(@dogrun), notice: '新規投稿できました。' 43 else 44 render :new, status: :unprocessable_entity 45 end 46 end 47 48 # PATCH/PUT /dogruns/1 or /dogruns/1.json 49 def update 50 @dogrun = Dogrun.find(params[:id]) 51 if @dogrun.update(dogrun_params) 52 redirect_to dogrun_url(@dogrun), notice: '更新できました。' 53 else 54 render :edit, status: :unprocessable_entity 55 end 56 end 57 58 # DELETE /dogruns/1 or /dogruns/1.json 59 def destroy 60 @dogrun = Dogrun.find(params[:id]) 61 @dogrun.destroy 62 redirect_to dogruns_url, notice: '削除しました。' 63 end 64 65 private 66 67 # Only allow a list of trusted parameters through. 68 def dogrun_params 69 params.require(:dogrun).permit(:dogrun_name, :image, :address, :price, :pr, :area) 70 end 71 72 def set_q 73 @q = Dogrun.ransack(params[:q]) 74 end 75end 76
app/views/likes/_like.html.erb
1 2<% if user_signed_in? %> 3 <% if current_user.liked_by?(dogrun.id) %> 4 <td> 5 <%= link_to destroy_like_path(dogrun), method: :DELETE, remote: true do %> 6 <i class="fa fa-heart unlike-btn"></i> 7 <% end %> 8 <%= dogrun.likes.count %> 9 </td> 10 <% else %> 11 <td> 12 <%= link_to create_like_path(dogrun), method: :POST, remote: true do %> 13 <i class="fa fa-heart like-btn"></i> 14 <% end %> 15 <%= dogrun.likes.count %> 16 </td> 17 <% end %> 18<% end %> 19
app/views/layouts/application.html
1<!DOCTYPE html> 2<html> 3 <head> 4 (省略) 5 # 以下を追加する 6 <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.0/css/all.css" integrity="sha384-Bfad6CLCknfcloXFOyFnlgtENryhrpZCe29RTifKEixXQZ38WheV+i/6YWSzkz3V" crossorigin="anonymous"> 7 </head> 8 9 <body> 10 (省略) 11 </body> 12</html>
app/views/likes/destroy.js.erb
1 2$('#likes_buttons_<%= @dogrun.id %>').html("<%= j(render partial: 'likes/like', locals: {dogrun: @post}) %>");
app/views/likes/create.js.erb
1$('#likes_buttons_<%= @dogrun.id %>').html("<%= j(render partial: 'likes/like', locals: {post: @dogrun}) %>"); 2
app/javascript/packs/application.js
1// This file is automatically compiled by Webpack, along with any other files 2// present in this directory. You're encouraged to place your actual application logic in 3// a relevant structure within app/javascript and only use these pack files to reference 4// that code so it'll be compiled. 5 6//= require jquery 7require("@rails/ujs").start() 8// require("turbolinks").start() 9require("@rails/activestorage").start() 10require("channels") 11 12// Uncomment to copy all static images under ../images to the output folder and reference 13// them with the image_pack_tag helper in views (e.g <%= image_pack_tag 'rails.png' %>) 14// or the `imagePath` JavaScript helper below. 15// 16// const images = require.context('../images', true) 17// const imagePath = (name) => images(name, true)
app/views/dogruns/index.html.erb
1 2<% @dogruns.each do |dogrun| %> 3上略 4 5 # ブロック変数postが使用できる範囲内での任意の位置に以下を記述してください 6 <div id="likes_buttons_<%= dogrun.id %>"> 7 <%= render partial: 'likes/like', locals: {dogrun: dogrun} %> 8 </div> 9<% end %>
config/routes.rb
1Rails.application.routes.draw do 2 (省略) 3 post 'like/:id' => 'likes#create', as: 'create_like' 4 delete 'like/:id' => 'likes#destroy', as: 'destroy_like' 5end
回答1件
あなたの回答
tips
プレビュー