2015-09-16 44 views
0

我在Rails應用程序中使用AJAX,這樣當有人創建友誼按鈕更新。問題是有兩個地方可以創建友誼,每個地方都有一個稍微不同的設計,一個有較小的按鈕。基於當前路線改變js.erb?

我創建了兩個助手來顯示按鈕。

用戶助手

module UsersHelper 

def action_buttons(user) 
    case current_user.friendship_status(user) when "friends" 
     link_to(friendship_path(current_user.friendship_relation(user)), method: :delete, :remote => true, data: {disable_with: "<i class='fa fa-spinner fa-spin'></i> Canceling"}, class: "js-cancel-friendship-btn btn btn-danger btn-sm") do content_tag(:i, "", class: "fa fa-times fa-fw", style: "color: #ffffff;") + " Cancel Friendship" end 
    when "pending" 
     link_to(friendship_path(current_user.friendship_relation(user)), method: :delete, :remote => true, data: {disable_with: "<i class='fa fa-spinner fa-spin'></i> Canceling"}, class: "js-cancel-request-btn btn btn-danger btn-sm") do content_tag(:i, "", class: "fa fa-times fa-fw", style: "color: #ffffff;") + " Cancel Request" end 
    when "requested" 
     link_to(accept_friendship_path(current_user.friendship_relation(user)), method: :put, :remote => true, data: {disable_with: "<i class='fa fa-spinner fa-spin'></i> Accepting"}, class: "js-accept-friendship-btn btn btn-primary btn-sm") do content_tag(:i, "", class: "fa fa-plus fa-fw", style: "color: #ffffff;") + " Accept Friendship" end + 
     link_to(friendship_path(current_user.friendship_relation(user)), method: :delete, :remote => true, data: {disable_with: "<i class='fa fa-spinner fa-spin'></i> Declining"}, class: "js-decline-friendship-btn btn btn-danger btn-sm m-l-sm") do content_tag(:i, "", class: "fa fa-times fa-fw", style: "color: #ffffff;") + " Decline" end 
    when "not_friends" 
     link_to(friendships_path(user_id: user.id), method: :post, :remote => true, data: {disable_with: "<i class='fa fa-spinner fa-spin'></i> Requesting"}, class: "js-not-friend-btn btn btn-primary btn-sm") do content_tag(:i, "", class: "fa fa-plus fa-fw", style: "color: #ffffff;") + " Send Friend Request" end 
    end 
end 

def action_buttons_profile(user) 
    case current_user.friendship_status(user) when "friends" 
     link_to "Cancel Friendship", friendship_path(current_user.friendship_relation(user)), method: :delete, :remote => true, class: "js-cancel-friendship-btn mr10" 
    when "pending" 
     link_to "Cancel Request", friendship_path(current_user.friendship_relation(user)), method: :delete, :remote => true, class: "js-cancel-request-btn mr10" 
    when "requested" 
     link_to("Accept Friendship", accept_friendship_path(current_user.friendship_relation(user)), method: :put, :remote => true, class: "js-accept-friendship-btn btn btn-primary btn-xs") + 
     link_to("Decline", friendship_path(current_user.friendship_relation(user)), method: :delete, :remote => true, class: "js-decline-btn btn btn-gold btn-xs") 
    when "not_friends" 
     link_to "Request Friendship", friendships_path(user_id: user.id), method: :post, :remote => true, class: "js-not-friend-btn btn btn-primary btn-sm mr10" 
    end 
end 

end 

我的問題是,js.erb文件(創建/接收/破壞)總是注入一個幫手。知道輸出到哪個頁面並不夠智能。有沒有辦法切換包含助手運行的js.erb文件的第2行?還是應該把這個開關放在別的地方?我不想在控制器中重複創建和銷燬方法,只是爲了包含這一點小差異。

Create.js.erb

$('.js-not-friend-btn').bind('ajax:success', function() { 
    $('.action-button-for-<%= @user.id %>').html('<%= j action_buttons(@user) %>'); 
    $('.js-pending-count').html('<%= @pending_count %>'); 
    $('.js-not-friend-btn').unbind(); 
}); 

toastr.options = { 
closeButton: true, 
progressBar: true, 
showMethod: 'slideDown', 
preventDuplicates: true, 
timeOut: 3000 
}; 
toastr.success('','Friendship Requested!'); 

友誼控制器

def create 
    @friendship = current_user.request_friendship(@user) 
    respond_to do |format| 

     get_counts() 
     if @user.email_notify_friend_received 
      ApplicationMailer.delay.friendship_requested(current_user,@user) 
     end 

     format.html {redirect_to users_path, notice: "Friendship Requested"} 
     format.js 
    end 
end 

def destroy 
    @user = @friendship.user == current_user ? @friendship.friend : @friendship.user 

    Friendship.transaction do 
     @friendship.destroy 
     a = Subscription.where(user_id: @friendship.friend, subscribe_to_id: @friendship.user) 
     a.first.destroy unless a.first.nil? 
     b = Subscription.where(user_id: @friendship.user, subscribe_to_id: @friendship.friend) 
     b.first.destroy unless b.first.nil? 
    end 

    respond_to do |format| 
     format.html {redirect_to users_path, notice: "Friendship Deleted"} 
     format.js 
    end 
end 
+0

你能不能只用'<%如果@some_condition_from_the_controller%>'插入一個JavaScript語句''<% else %>插入另一JavaScript語句''<% end %>在你創建。 js.erb? – mccannf

+0

這是有道理的,但不知道當兩個助手使用相同的控制器時如何傳遞額外的東西。 –

+0

所以你沒有說過你會怎麼知道在哪裏顯示一組動作按鈕。控制器是否知道這一點 - 即它使用這樣的按鈕呈現一頁而使用另一組按鈕的另一頁?如果是這樣,你可以使用該條件邏輯嗎?或者是同一頁上的兩組按鈕*? – mccannf

回答

0

我只是想出瞭如何解決這個問題。在助手中,我改變了我用來觸發JS的按鈕的類。然後,我在create.js.erb文件中有兩個塊,每個按鈕一個。現在頁面上的按鈕會被更新,而另一個會被忽略。

新Create.js.erb

$('.js-not-friend-btn').bind('ajax:success', function() { 
$('.action-button-for-<%= @user.id %>').html('<%= j action_buttons(@user) %>'); 
$('.js-pending-count').html('<%= @pending_count %>'); 
$('.js-not-friend-btn').unbind(); 
}); 

$('.js-profile-not-friend-btn').bind('ajax:success', function() { 
$('.action-button-for-<%= @user.id %>').html('<%= j action_buttons_profile(@user) %>'); 
$('.js-pending-count').html('<%= @pending_count %>'); 
$('.js-not-friend-btn').unbind(); 
}); 

toastr.options = { 
closeButton: true, 
progressBar: true, 
showMethod: 'slideDown', 
preventDuplicates: true, 
timeOut: 3000 
}; 
toastr.success('','Friendship Requested!');