PlankTon
PlankTon

Reputation: 12605

Rails: Determining RESTful http method by action type

I'm just wondering if rails has any inbuilt way of determining the correct http method (get, post, put, delete) by the action name(?)

So, for example, if I were to enter "new" as the action, is there any way rails can realise it ought request via "get"?

If not, a second problem...I tried manually creating a hash that mapped restful actions to their default methods - specifically:

actionhash = {
  :index => get,
  :show => get,
  :new => get,
  :create => post,
  :edit => get,
  :update => put,
  :delete => delete
}

That hash is then called with (if action = :show for example)

actionhash[action] action, :id => 123

The difficulty is when I create a hash with 'get' values, it comes back with:

undefined local variable or method `get' for #>

So, assuming there's no inbuilt way of determining the correct method, how can I enter http methods into a hash?

Upvotes: 0

Views: 377

Answers (1)

kain
kain

Reputation: 5570

actionhash = {
  :index => :get,
  :show => :get,
  :new => :get,
  :create => :post,
  :edit => :get,
  :update => :put,
  :delete => :delete
}

send(actionhash[action], :id => 123)

Ugly, though.

Upvotes: 1

Related Questions