https://rails-docker-portfolio2.herokuapp.com
gtihub : https://github.com/takoyan33/railsdock4
一点エラーに関する質問ですが、今
https://qiita.com/nojinoji/items/2c66499848d882c31ffa
を参考にしていいね機能を実装しています。
posts部分は、boardに置き換えて作っているのですが、
全部コードを貼り付けた後に、実際にいいねボタンを押した後写真のエラーが出ました。
createと書くと、エラーは起きないですが、何も変化しない感じです。
よろしくお願いします。
#エラーメッセージ ActiveRecord::RecordInvalid in LikesController#create Validation failed: Boards must exist, Users must exist Extracted source (around line #3): class LikesController < ApplicationController def create @like = @current_user.likes.create!(board_id: params[:board_id]) redirect_back(fallback_location: root_path) end Rails.root: /app
Ruby
#boards/show.html.erb <div class="d-flex align-items-center mt-4 mb-4"> <div class="ml-auto boards__linkBox"> <%= link_to '一覧', boards_path, class: 'btn btn-outline-dark' %> <%= link_to '編集', edit_board_path(@board), class: 'btn btn-outline-dark' %> </div> </div>
Ruby
<% if flash[:notice] %> <div class="alert alert-primary"><%= flash[:notice] %></div> <% end %> <%= render @board %> <% if @current_user %> <div class="p-comment__list"> <div class="p-comment__listTitle">コメント <%= @board.comments.count %>件</div> <%= render @board.comments %> </div> <%= render partial: 'comments/form', locals: { comment: @comment } %> <% else %> <div class="p-comment__list"> <div class="p-comment__listTitle">コメントはログインするとできるようになります。</div> </div> <% end %>
Ruby
<h3>いいね件数: <%= @board.likes.count %></h3> <% if @current_user.already_liked?(@board) %> <%= button_to 'いいねを取り消す', board_like_path(@board), method: :delete %> <% else %> <%= button_to 'いいね', board_likes_path(@board) %> <% end %> <h2>いいねしたユーザー</h2> <%# @board.liked_users.each do |user| %> <li><%#= user.email %></li> <%# end %> likes_controller.rb class LikesController < ApplicationController def create @like = @current_user.likes.create!(board_id: params[:board_id]) redirect_back(fallback_location: root_path) end def destroy @like = Like.find_by(board_id: params[:board_id], user_id: @current_user.id) @like.destroy redirect_back(fallback_location: root_path) end end board.rb
Ruby
#Board.rb class Board < ApplicationRecord has_many :comments, dependent: :delete_all has_many :board_tag_relations, dependent: :delete_all has_many :tags, through: :board_tag_relations has_one_attached :image belongs_to :user , optional: true has_many :likes, dependent: :destroy has_many :liked_users, through: :likes, source: :user # validates :name, length: { maximum: 10 } validates :title, presence: true, length: { maximum: 30 } validates :body, presence: true, length: { maximum: 1000 } scope :created_today, -> { where(created_at: Time.zone.now.all_day) } # 今日 scope :created_last_month, -> { where(created_at: Time.zone.now.prev_month.all_day) } # 1ヶ月前の投稿 scope :created_month, -> { where(created_at: Time.zone.now.all_month) } # 今月の投稿 end
Ruby
#like.rb class Like < ApplicationRecord belongs_to :boards belongs_to :users validates_uniqueness_of :board_id, scope: :user_id end
Ruby
user.rb class User < ApplicationRecord has_secure_password has_many :boards, dependent: :destroy has_many :comments, dependent: :destroy has_many :likes, dependent: :destroy has_many :liked_boards, through: :likes, source: :post def already_liked?(board) self.likes.exists?(board_id: board.id) end validates :name, presence: true, uniqueness: true, length: {maximum:16} validates :password, length: {minimum: 8 } end
boards_controller.rb def show @comment = Comment.new(board_id: @board.id) @user = User.find(@board.user_id) @like = Like.new #これのみ追加 end
RUby
routes.rb resources :boards do resources :likes, only: [:create, :destroy] #これのみ追加 end
まだ回答がついていません
会員登録して回答してみよう