Rails AJAX是一种破坏行为

时间:2015-05-23 10:34:22

标签: ajax ruby-on-rails-4

我正在尝试在友谊列表中使用AJAX,其中“请求友谊”按钮变为“取消请求”按钮,然后如果再次单击则再次返回。第一次点击有效,“取消请求”按钮上的ajax不会。

在destroy.js.erb的第2行,我得到了一个:

undefined method `id' for nil:NilClass

我知道它没有用,因为user_id没有传递给destroy的URL,就像创建它一样,但我不知道如何修复它。

友谊控制器

before_action :authenticate_user!
    before_action :set_user, only: [:create]
    before_action :set_friendship, only: [:destroy, :accept]

    def create
        @friendship = current_user.request_friendship(@user)
        respond_to do |format|
            format.html {redirect_to users_path, notice: "Friendship Requested"}
            format.js
        end
    end

    def destroy
        @friendship.destroy
        respond_to do |format|
            format.html {redirect_to users_path, notice: "Friendship Removed"}
            format.js
        end
    end

    def accept
        @friendship.accept_friendship
        @friendship.create_activity key: 'friendship.accepted', owner: @friendship.user, recipient: @friendship.friend
        @friendship.create_activity key: 'friendship.accepted', owner: @friendship.friend, recipient: @friendship.user
        respond_to do |format|
            format.html {redirect_to users_path, notice: "Friendship Accepted"}
            format.js
        end
    end

    private

    def set_user
        @user = User.find(params[:user_id])
    end

    def set_friendship
        @friendship = Friendship.find(params[:id])
    end

创建JS

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

销毁JS

$('.js-cancel-request-btn').bind('ajax:success', function() {
 $('.action-button-for-<%= @user.id %>').html('<%= j action_buttons(@user) %>');
});

用户帮助

def action_buttons(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 btn btn-danger btn-xs mr10"
    when "pending"
        link_to "Cancel Request", friendship_path(current_user.friendship_relation(user)), method: :delete, :remote => true, class: "js-cancel-request-btn btn btn-danger btn-xs 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-success btn-xs mr10") +
        link_to("Decline", friendship_path(current_user.friendship_relation(user)), method: :delete, :remote => true, class: "js-decline-friendship-btn btn btn-default btn-xs  mr10")
    when "not_friends"
        link_to "Add as Friend", friendships_path(user_id: user.id), method: :post, :remote => true, class: "js-not-friend-btn btn btn-success btn-xs mr10"
    end
end

感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:0)

为了在destroy.js.erb中使用@user,我必须在我的destroy操作中添加以下行:

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