Issue
I am currently using a partial to render a like/unlike button onto 2 different views: a topics view, and a user_bookmarks view. I do not know how to redirect to the topics view when the like/unlike button is clicked on the topics view pages and redirect to the user_bookmarks view when the like/unlike button is clicked there when the code for the like/unlike button is being pulled from a partial.
Here is my partial code:
<% show_remove_button ||= false %>
<li><%= bookmark.url %></li>
<div class="like">
<% if show_remove_button %>
<%= link_to "Remove", user_bookmark_path(get_user_bookmark_for(bookmark)), method: :delete %>
<% else %>
<% if like = current_user.liked(bookmark) %>
<%= link_to [bookmark, like], method: :delete do %><u>Unlike</u><% end %>
<% else %>
<%= link_to [bookmark, Like.new], method: :post do %><u>Like</u><% end %>
<% end %>
<% end %>
</div>
Here is my topics view:
<h1>All Bookmarks</h1>
<ul class="topics">
<% @topics.each do |topic| %>
<li><%= link_to "##{topic.name}", topic %></li>
<ul class="bookmarks">
<%= render topic.bookmarks %>
</ul>
<% end %>
</ul>
<br>
Here is my user_bookmarks view:
<h1>My Bookmarks</h1>
<ul class="topics">
<% @topics.each do |topic| %>
<li><%= link_to "##{topic.name}", topic %></li>
<ul class="bookmarks">
<% topic.bookmarks.each do |bookmark| %>
<%= render partial: "bookmarks/bookmark", object: bookmark, locals: {show_remove_button: true} %>
<% end %>
</ul>
<% end %>
</ul>
<br>
<h1>My Likes</h1>
<ul class="topics">
<% @liked_topics.each do |topic| %>
<li><%= link_to "##{topic.name}", topic %></li>
<ul class="bookmarks">
<% topic.bookmarks.each do |bookmark| %>
<%= render partial: "bookmarks/bookmark", object: bookmark, locals: {show_remove_button: false} %>
<% end %>
</ul>
<% end %>
</ul>
<br>
And here is my likes controller:
class LikesController < ApplicationController
def create
@bookmark = Bookmark.find(params[:bookmark_id])
like = current_user.likes.build(bookmark: @bookmark)
if like.save
flash[:notice] = "Liked bookmark"
redirect_to topics_path
else
flash[:error] = "Unable to add like. Please try again."
redirect_to topics_path
end
end
def destroy
@bookmark = Bookmark.find(params[:bookmark_id])
like = current_user.likes.find(params[:id])
if like.destroy
flash[:notice] = "Removed like."
redirect_to topics_path
else
flash[:error] = "Unable to remove like. Please try again."
redirect_to topics_path
end
end
end
Thanks in advance for your help!
Solution
I finally learned a simple fix to my problem using redirect_to :back
. Here it is in my likes controller:
class LikesController < ApplicationController
def create
@bookmark = Bookmark.find(params[:bookmark_id])
like = current_user.likes.build(bookmark: @bookmark)
if like.save
flash[:notice] = "Liked bookmark"
redirect_to :back
else
flash[:error] = "Unable to add like. Please try again."
redirect_to :back
end
end
def destroy
@bookmark = Bookmark.find(params[:bookmark_id])
like = current_user.likes.find(params[:id])
if like.destroy
flash[:notice] = "Removed like."
redirect_to :back
else
flash[:error] = "Unable to remove like. Please try again."
redirect_to :back
end
end
end
Answered By - Valerie Mettler Answer Checked By - Pedro (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.