栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

用户帖子的rails ajax fav按钮

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用户帖子的rails ajax fav按钮

首先,您需要设置数据库来处理此问题,就我个人而言,我将使用has_many:through关联,因为它比has_and_belongs_to_many具有更大的灵活性。但是,选择取决于您。我建议您在API中查找不同的类型,然后自己决定。此示例将处理has_many:through。

楷模

# user.rb (model)has_many :favoriteshas_many :posts, :through => :favorites# post.rb (model)has_many :favoriteshas_many :users, :through => :favorites# favorite.rb (model)belongs_to :userbelongs_to :post

控制者

# favorites_controller.rbdef create  current_user.favorites.create(:post_id => params[:post_id])  render :layout => falseend

路线

match "favorites/:post_id" => "favorites#create", :as => :favorite

jQuery的

$(".favorite").click(function() {  var post_id = $(this).attr('id');  $.ajax({    type: "POST",    url: 'favorites/' + post_id,    success: function() {      // change image or something    }  })})

笔记

这假设了两件事:使用Rails 3,使用jQuery,每个喜欢的图标都有一个带有帖子ID的html
ID。请记住,我尚未测试代码,而是在此窗口中编写的,因此您可能必须修复一些小问题,但它应该使您对
通常的操作方式有一个印象。视觉的东西,等等,我会留给你的。

如果有人发现任何错误,请随时编辑此帖子。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/616692.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号