为什么`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之间进行转换。
答
对于在从Rails 2.x转换时出现此问题的任何人,值得注意的是request.method调用用于返回符号。
我一直在寻找在错误的文档:) – Zabba 2011-03-02 20:27:47