介绍收藏功能的实现。
首先是建立关联关系,一个用户会收藏多篇文章,每篇文章又可以被多个用户收藏:
class User
has_many:articles,dependent: :destroy
has_many:favorites
end
class Article
belongs_to:user
has_many:favorites
end
class Favotite
belongs_to:user
belongs_to:article
end
我们可以对文章进行收藏,也可以取消收藏,因此需要两个控制方法来实现收藏和取消收藏的功能:
#app/controller/favorites_controller.rb
classFavoritesController<ApplicationController
def create
@article=Article.find(params[:article_id])
current_user.favorites.create(article_id: params[:article_id])
redirect_to:back
end
def destroy
favorite=Favorite.find(params[:id])
@article=favorite.article
favorite.destroy
redirect_to:back
end
end
redirect_to :back 的意思是返回到前一个页面,所以玩家点击收藏或取消收藏的链接时,不会跳转到新的页面,而是依然回到原页面。
然后添加相应的路由:
resources:favorites,only: [:create,:destroy]
为了让代码清晰,收藏标签的代码使用了一个好单独的 partial:
#app/views/favorites/_favorite_link.html.erb
<%iffavorite=current_user.favorites.where(article_id: @article.id).first%><%=link_tofavorite_path(favorite),method: :deletedo%>
<spanclass="glyphicon glyphicon-bookmark"></span><%end%>
<%else%>
<%=link_tofavorites_path(article_id: @article.id),method: :postdo%>
<spanclass="glyphicon glyphicon-bookmark favorite_color"></span>
<%end%>
<%end%>
这样一来,一个简单的收藏功能就实现了。但是该功能存在一个致命的缺陷,即每次点击收藏或取消收藏链接的时候,整个页面都会被刷新,这显然是无法接受的,这时就要用到 Ajax 大显身手了,应用中有很多地方都要用到 Ajax 这项技术,因此整理之后我会用单独的篇幅来描述。
网友评论