加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails-3 – 为什么`request.method`返回一个字符串(而不

发布时间:2020-12-16 19:33:20 所属栏目:百科 来源:网络整理
导读:我认为request.method应该返回一个符号,如:get,:put等? 但是在控制器动作中,我得到GET作为一个字符串! 难道我做错了什么? 在routes.rb中: resources :posts member do get 'some_action' endend 在.erb视图中: %= link_to "Some Action",some_action_
我认为request.method应该返回一个符号,如:get,:put等?
但是在控制器动作中,我得到GET作为一个字符串!

难道我做错了什么?

在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

PS.我在Ruby 1.8.7 p330上使用Rails 3.0.3

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读