Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is `request.method` returning a string (instead of a symbol)?

I thought request.method is supposed to return a symbol like :get, :put etc. ? But instead in the controller action, I am getting GET as a String!

Am I doing something wrong?

In routes.rb:

resources :posts
  member do
    get 'some_action'
  end
end

In a view .erb:

<%= link_to "Some Action",some_action_post_path %>

In PostsController:

def some_action
  p request.method               # => "GET"
  p request.method.class.name    # => "String"
  if request.method == :get
    #does not get called
  end
end

Ps. I'm using Rails 3.0.3 on Ruby 1.8.7 p330

like image 281
Zabba Avatar asked Mar 02 '11 17:03

Zabba


3 Answers

Works as designed - it is supposed to return a string :) So, use the string. Different topic: you can convert between strings and syms with to_s and to_sym, respectively.

like image 120
Mörre Avatar answered Oct 15 '22 14:10

Mörre


For anyone coming at this question while converting from Rails 2.x, it's worth noting that the request.method call used to return symbols.

like image 20
douglasr Avatar answered Oct 15 '22 14:10

douglasr


Old question, but we also have these now:

http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-method_symbol http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-request_method_symbol

like image 43
Ross Avatar answered Oct 15 '22 15:10

Ross