2015-05-23 4 views
0

Я пытаюсь использовать AJAX в списке друзей, где кнопка «Запрос дружбы» превращается в кнопку «Отменить запрос», а затем обратно, если щелкнуть второй раз. Первый щелчок работает, ajax на кнопке «Отменить запрос» нет.Rails AJAX a Destroy Action

В строке 2 destroy.js.erb я получаю:

undefined method `id' for nil:NilClass 

Я знаю, что это не работает, потому что user_id не передается в 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 

Спасибо за любую помощь вы можете предложить.

ответ

0

мне пришлось добавить следующую строку в моих уничтожить действия для того, чтобы использовать @user в destroy.js.erb:

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