使用params [:foo]和@foo有什么区别?

蒂米·冯·海斯(Timmy Von Heiss)
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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

直接使用shell命令(例如foo)和使用$(foo)有什么区别?

“使用名称空间栏”和“使用Bar :: Foo”之间有什么区别?

在React组件中,foo(){}和bar =()=> {}之间有什么区别,我什么时候应该使用?

&foo :: function和foo :: function有什么区别?

Bash 中的 `declare foo` 和 `foo=` 有什么区别?

ifeq($(foo),)和ifndef foo有什么区别

location / foo和location ^〜/ foo有什么区别

Clojure中的:foo,:: foo,:: bar / foo和:bar / foo有什么区别?

方法调用语法`foo.method()`和UFCS`Foo :: method(&foo)`有什么区别?

Throw'foo',throw Error('foo'),throw new Error('foo')和有什么区别?

python中装饰器中的foo=bar(foo)和something=bar(foo)有什么区别?

使用echo; >和>>有什么区别

Postman中的Params和Body有什么区别

“ $ {foo.bar}”和“#{foo.bar}”之间有什么区别?

$ bar = boolval($ foo)和$ bar =(bool)$ foo在php中有什么区别?

“git merge foo”和“git merge origin/foo”有什么区别?

当sub Foo :: bar {}和sub bar {}都属于包Foo时,有什么区别?

foo(int arr [])和foo(int arr [10])有什么区别?

“ foo is None”和“ foo == None”之间有什么区别吗?

Scala中的s“ foo $ bar”和“ foo%s” .format(bar)有什么区别

func foo(arr [] int)int和func foo(arr [num] int)int有什么区别?

NativeScript-viewModel.foo和viewModel.get(“ foo”)有什么区别?

char * str = {“ foo”,...}和char str [] [5] = {“ foo”,...}数组定义之间有什么区别?

TypeScript中的类型化数组-Array <Foo>和Foo []有什么区别?

$ foo ['bar'] =“ Value”和$ foo [bar] =“ Value”有什么区别

CSS类.foo.bar(不带空格)和.foo .bar(带空格)有什么区别?

foo.toString()和Object.prototype.toString.call(foo)有什么区别?

在 C++ 类成员函数中调用 foo() 和 ::foo() 有什么区别?

在Python中从`foo.py`导入和`foo / __ init __。py`导入之间有什么区别