我目前正在使用partial来将一个like /不同的按钮渲染到两个不同的视图上:一个主题视图和一个user_bookmarks视图。我不知道如何在主题视图页面上单击like /不同按钮时重定向到主题视图,并在类似/不同按钮的代码被点击时,当点击like /不同按钮时重定向到user_bookmarks视图从部分拉出来。
这是我的部分代码:
<% 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>
以下是我的主题观点:
<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>
这是我的user_bookmarks视图:
<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
提前感谢您的帮助!
答案 0 :(得分:1)
如果我正确理解了问题,您想要根据您所在的视图切换呈现的视图。
您可以使用以下命令返回您来自的路径。
redirect_to URI(request.referrer).path
request.referrer是 request.env的快捷方式[&#39; REQUEST_REFERRER&#39;]
假设每个视图都有自己的路径,我会比较当前路径以决定做什么。
在您的控制器中,例如:
if like.save
flash[:notice] = "Liked bookmark"
else
flash[:error] = "Unable to add like. Please try again."
end
redirect_to URI(request.referrer).path
您会注意到我还从if语句中取出了重定向,因为它始终是阻止之后可以说明的终点。这是DRY代码的好习惯。
或者你可以设置条件,即
redirect_to URI(request.referrer).path == topic_path ? topic_path : bookmark_path
如果您在被称为三元运营商之前没有看到这一点。
condition ? do_this_if_true : do_this_if_false
在上面的上下文中,想象它是这样的:
if URI(request.referrer).path == topic_path
topic_path
else
bookmark_path
end
答案 1 :(得分:1)
我终于使用redirect_to :back
学习了对问题的简单修复。这是我喜欢的控制器:
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