Rails 一个检查 params 参数的纠结问题

linjunhalida · May 23, 2013 · Last by luikore replied at May 23, 2013 · 2149 hits

很多时候,在 controller 里面要验证一个数据是否有效的,比如:

class PostController < ActionController::Base
  def operate
    raise Http404 unless Post::TYPES.include params[:type].to_sym
  end
end

ruby 里面 symbol 不释放,这样就会有一个安全隐患,输入无数错误的 type,内存泄露掉。

但是每次把 Post::TYPES 全部变成 string 再做 include?判断,又很不优雅,大家觉得应该怎么做比较好?

最好一开始就是 string array... 要改的地方太多就先暂时 find{|ty| ty.to_s == params[:type]} ...

You need to Sign in before reply, if you don't have an account, please Sign up first.