エラーメッセージ
Started GET "/talks" at 2021-01-10 13:28:32 +0900 Processing by TalksController#index as HTML Rendering talks/index.html.slim within layouts/application Talk Load (5.9ms) SELECT "talks".* FROM "talks" ORDER BY "talks"."created_at" DESC ↳ app/views/talks/index.html.slim:9 User Load (3.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 2], ["LIMIT", 1]] ↳ app/views/talks/index.html.slim:13 User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]] ↳ app/helpers/sessions_helper.rb:20:in `current_user' User Exists? (4.6ms) SELECT 1 AS one FROM "users" INNER JOIN "likes" ON "users"."id" = "likes"."user_id" WHERE "likes"."talk_id" = $1 AND "users"."id" = $2 LIMIT $3 [["talk_id", 17], ["id", 1], ["LIMIT", 1]] ↳ app/models/talk.rb:16:in `like?' Rendered talks/index.html.slim within layouts/application Completed 500 Internal Server Error in 69ms
詳細画面は正常に動きます。
環境
rails 6.0.2 ubuntu (wsl)
該当ソースコード
routes.rb
ruby
1 resources :talks do 2 resources :likes, only: %i(create destroy) 3 resources :comments 4 end 5 6--------------------------------------------------- 7 talk_likes POST /talks/:talk_id/likes(.:format) likes#create 8 talk_like DELETE /talks/:talk_id/likes/:id(.:format) likes#destroy
talks_controller.rb
ruby
1 def index 2 unless params[:q].blank? 3 split_keyword = params[:q][:content_cont].split(/\p{blank}/) 4 end 5 @q = Talk.ransack(content_cont_any: split_keyword) 6 @talks = @q.result.recent 7 end
likes_controlle.rb
ruby
1 def create 2 @like = current_user.likes.build(talk_id: params[:talk_id]) 3 @like.save 4 redirect_back(fallback_location: root_path) 5 end 6 7 def destroy 8 @like = Like.find_by(talk_id: params[:talk_id], user_id: current_user.id) 9 @like.destroy 10 redirect_back(fallback_location: root_path) 11 end
talks/index.html.slim
slim
1- @talks.each do |talk| 2 .card.mx-auto.talk-card 3 .card-body 4 ・・・省略 5 .card-text 6 = link_to simple_format(h(talk.content), {}, sanitize: false, wrapper_tag: "div"), talk 7 .row.button-area 8 = link_to talk, class: "btn-gradient-radius" 9 i.far.fa-comment 10 span コメント 11 - if talk.like?(current_user) 12 = link_to "##", talk_like_path(talk), method: :delete 13 - else 14 = link_to "#", talk_likes_path(talk), method: :post 15
talk.rb
belongs_to :user has_many :likes, dependent: :destroy has_many :liked_users, through: :likes, source: :user validates :user_id, presence: true validates :content, presence: true acts_as_taggable scope :recent, -> { order(created_at: "DESC") } def like?(user) liked_users.include?(user) end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/01/10 04:24 編集
2021/01/10 03:01
2021/01/10 04:32