###はじめに
いいね機能実装時のエラーが解決できないので質問させていただきます。
参考にさせていただいたサイトはhttps://techtechmedia.com/favorite-function-rails/このサイトです。
エラーがよく理解できないのでそちらを教えて欲しいのと、どのようにすれば解決するかおしえてほしいです。
いいねのリンクを押したら検証ツールで下記のエラーが出ます。
###エラー文
POST http://localhost:3000/like/7 500 (Internal Server Error)
./node_modules/@rails/ujs/lib/assets/compiled/rails-ujs.js.Rails.ajax @ rails-ujs.js:216
rails-ujs
if (xhr.readyState === XMLHttpRequest.OPENED) { return xhr.send(options.data);⇦⇦⇦⇦⇦ここのコードで赤いバツが出てる }
###自分で行ったこと
変数の受け渡しが間違っているかと思い、変数名を変更してみたがなにもかわらなかった
###コード
routes.rb
Rails.application.routes.draw do devise_for :users root to: "items#index" resources :items, only: [:index, :new, :create, :show, :edit, :update] do collection do get 'post','search','modal' end end resources :profiles, only: [:show, :new, :edit, :create, :update] do member do get 'show_user' end end post 'like/:id' => 'likes#create', as: 'create_like' delete 'like/:id' => 'likes#destroy', as: 'destroy_like' end
user.rb
class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable VALID_PASSWORD_REGEX = /\A(?=.*?[a-z])(?=.*?\d)[a-z\d]{6,20}+\z/i.freeze validates :password, presence: true validates :password, format: { with: VALID_PASSWORD_REGEX }, allow_blank: true has_many :items has_one :profile, dependent: :destroy has_many :likes, dependent: :destroy has_many :like_items, through: :likes, source: :item def liked_by?(item_id) likes.where(item_id: item_id).exists? end end
like.rb
class Like < ApplicationRecord belongs_to :user belongs_to :item end
index.html.erb
<% @items.each do |item| %> <tr id="item_<%= item.id %>"> <%= render "shared/item", item: item %> </tr> <% end %>
_item.html.erb
<div class='item-contents'> <div class = 'timeline'> <div class = 'timeline-column'> <div class = 'image-field'> <%= image_tag(item.image , class:'timeline-image') %> </div> <div class = 'item-body'> <div class = 'item-body-contents'> <div class= 'item-profile'> <div class= 'item-profile-icon'> <% if item.profile.image.present? %> <%= link_to image_tag(item.profile.image, class:'mini-image') , "/profiles/#{item.profile.id}/show_user" %> <% else %> <span class= 'image-text' >NO IMAGE</span> <% end %> </div> <div class= 'item-profile-name'> <p class= 'mini-name'> <%= item.profile.name %> </p> </div> <% if current_user.liked_by?(item.id) %> <td><%= link_to 'いいねを外す', destroy_like_path(item), method: :DELETE, remote: true %><%= item.likes.count %></td> <% else %> <td><%= link_to 'いいね', create_like_path(item), method: :POST, remote: true %><%= item.likes.count %></td> <% end %> <% if item.user_id == current_user.id %> <div class = 'item-option'> <div class = 'item-option-btn' > <%= link_to('#', class: 'option-btn') do%> <%= image_tag 'gear_icon.png' ,class: 'item-btn-icon'%> <% end %> </div > </div> <% end %> </div> <div class = 'item-detail'> <p class = 'detail-title'>Title</p> <p class = 'detail-message'> <%= item.title %> </p> </div> <div class = 'item-detail'> <p class = 'detail-title'>Category</p> <p class = 'detail-message'> <%= item.category.name %> </p> </div> <div class = 'item-detail-tag'> <p class = 'detail-title'>Tag</p> <p class = 'detail-message'> <% unless item.tags.empty? %> <% item.tags.each do |tag| %> #<%=tag.tag_name %> <%end %> <% else %> 未記入 <% end %> </p> </div> <div class = 'item-information'> <p class = 'detail-title'>Information</p> <p class = 'detail-message-information'> <% unless item.information.empty? %> <%= item.information %> <% else %> 未記入 <% end %> </p> </div> <div class= 'post-time-box'> <p class= "post-time">投稿日<%= item.created_at.to_s(:datetime_jp) %></p> </div> </div> </div> </div> </div> </div>
likes_controller.rb
class LikesController < ApplicationController before_action :item_params def create Like.create(user_id: current_user.id, item_id: params[:id]) end def destroy Like.find_by(user_id: current_user.id, item_id: params[:id]).destroy end private def item_params @item = Item.find(params[:id]) end end
create.js.erb
document.getElementById("item_<%= @item.id %>").innerHTML = '<%= j(render @item) %>'
destroy.js.erb
document.getElementById('item_<%= @item.id %>').innerHTML = '<%= j(render @item) %>'
回答2件
あなたの回答
tips
プレビュー