为什么`request.method`返回一个字符串(而不是一个符号)?

为什么`request.method`返回一个字符串(而不是一个符号)?

问题描述:

我以为request.method应该返回一个像:get,:put等符号? 但是,在控制器操作中,我将GET作为字符串!为什么`request.method`返回一个字符串(而不是一个符号)?

我做错了什么?

routes.rb

resources :posts 
    member do 
    get 'some_action' 
    end 
end 

在视图.erb:

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

PostsController

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

聚苯乙烯。我在Ruby 1.8.7上使用Rails 3.0.3 p330

按设计工作 - 它应该返回一个字符串:) 所以,使用字符串。不同的主题:您可以分别使用to_s和to_sym在字符串和syms之间进行转换。

+0

我一直在寻找在错误的文档:) – Zabba 2011-03-02 20:27:47

对于在从Rails 2.x转换时出现此问题的任何人,值得注意的是request.method调用用于返回符号。