ApplicationController
before_action :example_filter
def example_filter
params[:foo] = '1' if #somethinghere
@foo = '1' if #somethinghere
end
NewsController
if @foo == '1' #somethinghere
if params[:foo] == '1' #somethinghere
在这种情况下使用@foo或params [:foo]有什么区别或好处?
一个区别是用户可以在查询字符串中自己传递params [:foo]:
example.com/news?foo=1
的@foo
是对象构件。的params[:foo]
是请求PARAM。的params[:foo]
-可能不具有它的对象可以仅是字符串或字符串的数组(因为它从请求带来的)。
params[:foo] = 1
您编写的代码将覆盖请求参数。
最好使用这样的代码:
ApplicationController
before_action :example_filter
def example_filter
@foo = params[:foo]
@foo = 'something' if #somethinghere
end
# somewhere
if @foo == '1' #somethinghere
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句