Search code examples
ruby-on-railsrubyruby-on-rails-3ruby-on-rails-3.1

Custom Rest actions


I am trying to create a friendship and I created a custom action called accept. however i can't reach it. Everytime I call it i get the action show could not be found.

Here my route.rb file

  resources :friendships do
    collection do
      delete 'cancel'
      get 'accept'
    end
  end

Here how i call it

<%= link_to 'Accept', accept_friendships_path(:friend_id => f) %>

accept_friendships was taken from rake routes commands. And here how i define my accept controller

  #Accept friendships
  def accept
    if @customer.requested_friends.include?(@friend)
      Friendship.accept(@customer, @friend)
      flash[:notice] = "Friendship Accepted"
    else
      flash[:notice] = "No Friendship request"
    end
    redirect_to root_url
  end

Here the error

Unknown action

The action 'show' could not be found for FriendshipsController

Solution

  • Maybe I am wrong, but why you want "accept" to be a collection? I guess you want it to be a member, since you are passing friend_id. If you change it to member and make the path accept_friendship_path(@friendship) [ note singular form of friendship ], you might have better luck. Beside an addition argument your case does not differ from example on Ruby on Rails Guides, that is why it is worth to try it