<やりたいこと>
現在各ページごとにユーザーが自由に意見を投稿できるサービスを作っています。
そのポストにいいねをつけられるようにしたいと考えています。
progateを見ながら作っているのですが、そこではpostコントローラのindexに書き込みが羅列されており、それをクリックするとshowに飛び、いいねができるようになっていたのですが、私はindexにポストを羅列して、そこでいいねができるようにしたいのです。(私の場合はshowにコメントを羅列して、そこのポストにいいね!ボタンをつける)
<現状>
しかし、ユーザーがいいねをすると、そのページにある全てのポストにいいねしたことになってしまい、各ポストについて独立していいねすることができません。これをなんとかしたいです。
<アプリの構造>
Commentモデル(name:string) userモデル(name:string) postモデル(content:text) likeモデル(post_id:integer, user_id:integer)といった感じです。各モデルにはコントローラが存在しています。
commentコントローラのshowの各ページにフォームが用意されており、postコントローラのクリエイトでコメントを生成して、コメントコントローラのshowページで表示するようにしています。
comment.html.erb
1<% provide(:title, @comment.name+"のページ") %> 2 3<h1><%= @comment.name %>について何かを書こう!</h1> 4 5 6 7 <% @posts.each do |post| %> 8 <div class="posts-index-item"> 9 <strong>『<%= post.content %>』</strong> 10 11 12 13 14 <% if Like.find_by(user_id: @current_user.id, post_id: post.id) %> 15 <<%= link_to("/likes/#{post.id}/destroy", {method: "post"}) do %> 16 <span class="fa fa-heart like-btn-unlike"></span> 17 <% end %> 18 <% else %> 19 <%= link_to("/likes/#{post.id}/create", {method: "post"}) do %> 20 <span class="fa fa-heart like-btn"></span> 21 <% end %> 22 <% end %> 23 <%= @likes_count %> 24 <p align=right><%= post.created_at %></p> 25 </div> 26 <% end %> 27 28<% if logged_in? %> 29 <h1>投稿する</h1> 30 <%= form_tag("/posts/create") do %> 31 <textarea name="content"></textarea> 32 <%= hidden_field_tag "comment_id", @comment.id %> 33 <input type="submit" value="投稿"> 34 <% end %> 35 <% else %> 36 <h3><%= link_to "投稿するにはログインが必要です", login_path %></h3> 37 <% end %> 38
commentコントローラ
1class CommentsController < ApplicationController 2 3 4 def show 5 @comments = Comment.find_by(id: params[:id]) 6 @posts = Post.where(comment_id: @comment.id) 7 @current_user = User.find_by(id: session[:user_id]) 8 @post = Post.find_by(id: params[:id]) 9 @likes_count = Like.where(post_id: @post.id).count 10 end 11end
likesコントローラ
1class LikesController < ApplicationController 2 3 4 def create 5 @like = Like.new(user_id: @current_user.id, post_id: params[:post_id]) 6 @like.save 7 redirect_to("/posts/#{params[:post_id]}") 8 end 9 10 11 def destroy 12 @like = Like.find_by(user_id: @current_user.id, post_id: params[:post_id]) 13 @like.destroy 14 redirect_to("/posts/#{params[:post_id]}") 15 end 16 17end
postコントローラ
1class PostsController < ApplicationController 2 def index 3 @posts = Post.all.order(created_at: :desc) 4 end 5 6 def show 7 @post = Post.find_by(id: params[:id]) 8 @likes_count = Like.where(post_id: @post.id).count 9 end 10 11 def new 12 end 13 14 def create 15 @post = Post.new(content: params[:content], 16 user_id: @current_user.id, 17 comment_id: params[:comment_id]) 18 @post.save 19 redirect_to("/help") 20 21 end 22 23 24end
足りない情報があれば追加します。
よろしくお願いいたします。
Rails.application.routes.draw do post "likes/:post_id/create" => "likes#create" post "likes/:post_id/destroy" => "likes#destroy" get "posts/index" => "posts#index" get "posts/new" => "posts#new" get "posts/:id" => "posts#show" post "posts/create" => "posts#create" get 'comments/index' get 'comments/:id' => 'comments#show' root 'static_pages#home' get '/help', to: 'static_pages#help' get '/configuration', to: 'static_pages#configuration' get '/about', to: 'static_pages#about' get '/contact', to: 'static_pages#contact' get '/top', to: 'static_pages#top' get '/signup', to: 'users#new' get '/login', to: 'sessions#new' post '/login', to: 'sessions#create' delete '/logout', to: 'sessions#destroy' get '/:city/:banti', to: 'comments#index' resources :users do member do get :following, :followers end end resources :account_activations, only: [:edit] resources :password_resets, only: [:new, :create, :edit, :update] resources :microposts, only: [:create, :destroy] resources :relationships, only: [:create, :destroy] end

あなたの回答
tips
プレビュー