首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

remote 珍藏

2012-12-27 
remote 收藏view% javascript_include_tag jquery %% javascript_include_tag jquery_ujs %scr

remote 收藏
view

<%= javascript_include_tag 'jquery' %><%= javascript_include_tag 'jquery_ujs' %><script type="text/javascript">$(function(){    $('.favorite').bind('ajax:success', function() {        $(this).fadeOut();    });});</script>


<% unless FavEvent.where('event_id = ? and uid = ?', @event.id, current_uid).any? -%>  <%= link_to "收藏", "/user_center/fav_events?event_id=#{@event.id}", class: 'favorite', method: :post, remote: true %><% end -%>


routes.rb
namespace "user_center" do  resources :fav_eventsend


user_center/fav_events_controller.rb
def index  @fav_events = FavEvent.where('uid = ?',    current_uid).order('event_id desc').page(params[:page]).per(6)enddef create  unless FavEvent.where('event_id = ? and uid = ?', params[:event_id], current_uid).any?    event = Event.find(params[:event_id])    FavEvent.create(event_id: event.id, uid: current_uid)  end  respond_to do |format|    format.js   { render :nothing => true }  endenddef destroy  @fav_event = FavEvent.find(params[:id])  @fav_event.destroy  redirect_to user_center_fav_events_urlend



在有分页的收藏列表页 取消收藏

fav_events/index.html.erb
<ul id="fav_events">   <%= render @fav_events %></ul><div id="paginator">    <%= paginate @fav_events, :remote => true %></div>


fav_events/_fav_event.html.erb
<li>  <img src="<%= url('../../../images/common/perform_lists_photo.jpg') %>" alt="remote 珍藏" width="220" height="130" />  <p>    <%= link_to "取消收藏", "/user_center/fav_events/#{fav_event.id}",                class: 'cancel_favorite', method: :delete, remote: true %>  </p>   </li>


fav_events/index.js.erb
window.location.hash = '#fav_events';$('#fav_events').html('<%= escape_javascript render(@fav_events) %>');$('#paginator').html('<%= escape_javascript(paginate(@fav_events, :remote => true).to_s) %>');

热点排行