Javascript 根据当前路线更改js.erb?
我在Rails应用程序中使用AJAX,这样当有人创建友谊时,按钮就会更新。问题是有两个地方可以建立友谊,每个地方的设计都略有不同,一个地方的按钮更小 我已经创建了两个助手来显示按钮 用户助手Javascript 根据当前路线更改js.erb?,javascript,ruby-on-rails,ajax,Javascript,Ruby On Rails,Ajax,我在Rails应用程序中使用AJAX,这样当有人创建友谊时,按钮就会更新。问题是有两个地方可以建立友谊,每个地方的设计都略有不同,一个地方的按钮更小 我已经创建了两个助手来显示按钮 用户助手 我刚刚想出了解决这个问题的办法。在helpers中,我更改了用于触发JS的按钮的类。然后,在create.js.erb文件中,我有两个块,每个按钮一个。现在页面上的按钮将被更新,而另一个按钮将被忽略 新建Create.js.erb 你能不能在create.js.erb中使用insert-one javasc
我刚刚想出了解决这个问题的办法。在helpers中,我更改了用于触发JS的按钮的类。然后,在create.js.erb文件中,我有两个块,每个按钮一个。现在页面上的按钮将被更新,而另一个按钮将被忽略 新建Create.js.erb
你能不能在create.js.erb中使用insert-one javascript语句插入另一个javascript语句?这很有意义,但不确定当两个助手使用同一个控制器时如何传递额外的内容。因此,你没有说过如何知道在哪里显示一组操作按钮,而不是另一组。控制器是否知道这一点?即,它呈现一个带有此类按钮的页面,而呈现另一个带有不同按钮集的页面?如果是的话,你能使用条件逻辑吗?或者这两组按钮在同一页上?
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-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
$('.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!');