htmlの
<%= link_to("/likes/#{@post.id}/create", {method: "post"}) do %>
<span class="fa fa-heart like-btn-unlike"></span>
<% end %>
のところを押すと、なぜか/likes/createに飛んでしまいます。
/likes/ID/createに飛んでほしいです。
よろしくおねがいします。
html
1 <% if Like.find_by(user_id: @current_user.id, post_id: @post.id) %> 2 <%= link_to("/likes/#{@post.id}/create", {method: "post"}) do %> 3 <span class="fa fa-heart like-btn-unlike"></span> 4 <% end %> 5 <% else %> 6 <%= link_to("/likes/#{@post.id}/destroy", {method: "post"}) do %> 7 <span class="fa fa-heart like-btn"></span> 8 <% end %> 9 <% end %>
ruby
1 post "likes/:post_id/create" => "likes#create" 2 post "likes/:post_id/destroy" => "likes#destroy"
ruby
1class LikesController < ApplicationController 2 def create 3 @like = Like.new( 4 user_id: @current_user.id, 5 post_id: params[:post_id] 6 ) 7 @like.save 8 redirect_to("/posts/#{params[:post_id]}") 9 end 10 11 def destroy 12 @like = Like.find_by( 13 user_id: params[:user_id], 14 post_id: params[:post_id] 15 ) 16 @like.destroy 17 redirect_to("/posts/#{params[:post_id]}") 18 end 19 20end 21
あなたの回答
tips
プレビュー