Jseb
Jseb

Reputation: 1934

Custom Rails Actions

I am trying to create a cusotm actions in rails. I though it would of work but for some reason it doesn't see the action has show. I remember seing somewhere I might have to set something but I can't remember.

Here my route files

  resources :friends do
    collection do
      delete 'cancel'
    end
  end

In my rake route this generate the following

cancel_api_v1_friends DELETE /api/v1/friends/cancel(.:format)                           api/v1/friends#cancel

So when i go to ...api/v1/friends/cancel.json to see the api created by it say can't find show actions.

Is there something i missed?

This is the error I get

The action 'show' could not be found for Api::V1::FriendsController

My controller has the following method

def cancel
  ...
end

Upvotes: 0

Views: 107

Answers (3)

Jyothu
Jyothu

Reputation: 3144

Try this. change your route like

  resources :friends do
    member do
      delete :cancel
    end
  end

You can call this method by

cancel_friend_path(friend_object)

And write a method in FriendsController

def cancel
  whatever you wanna do.
end

Upvotes: 0

rony36
rony36

Reputation: 3339

You can use RESTClient to test this.

Here is an amazing FireFox RESTClient Add-ons.

With this addons you are able to request with GET,PUT, POST, DELETE ... etc methods.

Actually you need to request DELETE not GET as Matt answered previously. Hitting api/v1/friends/cancel.json you requesting GET method.

Upvotes: 0

Matt Allen
Matt Allen

Reputation: 196

If you're hitting /api/v1/friends/cancel.json in a browser, it'll be doing a GET not a DELETE and calling the show method with the ID of 'cancel'

To test this properly, use something like curl.

curl -i -H "Accept: application/json" -X DELETE http://localhost:3000/api/v1/friends/cancel.json

Matta

Upvotes: 1

Related Questions