根据当前路线更改js.erb?

时间:2015-09-16 09:17:35

标签: javascript ruby-on-rails ajax

我在Rails应用程序中使用AJAX,以便当有人创建友谊时按钮会更新。问题在于,有两个地方可以创造友谊,每个地方的设计略有不同,其中一个按钮较小。

我创建了两个帮助器来显示按钮。

用户帮助

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文件(create / accept / destroy)总是注入一个帮助器。要知道它输出的页面是不够聪明的。有没有办法切换包含帮助程序的js.erb文件的第2行?或者这个开关应该在别的地方?我不想在控制器中复制create和destroy方法只是为了包含这一点差异。

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

1 个答案:

答案 0 :(得分: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!');