Ruby - :variable 和 @variable 之间的区别 [英] Ruby - Difference between :variable and @variable

查看:33
本文介绍了Ruby - :variable 和 @variable 之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个 Ruby on Rails 新手,我理解@"和:"引用有不同的含义.我在 SO 中看到了这篇文章,其中描述了一些的差异.

As a Ruby on Rails newbie, I understand that the "@" and ":" references have different meanings. I saw this post in SO, which described some of the differences.

  1. @ 表示实例变量(例如,@my_selection)
  2. : 表示别名(例如 :my_selection)
  1. @ indicates a instance variable (e.g., @my_selection)
  2. : indicates an alias (e.g., :my_selection)

我遇到了一种情况,我有一个标准的 MVC 页面,类似于我的 web 应用程序中的所有其他表单/页面.

I ran into a situation where I had a standard MVC page, similar to all of the other forms/pages in my webapp.

html.erb 片段

<%= form_for @my_selection do |f| %>

route.rb 片段

resources :my_selections

当我尝试访问此页面时,出现此错误:

When I attempt to access this page, I get this error:

NoMethodError in selections#create
Showing C:/somedir/myapp/app/views/my_selections/index.html.erb where line #16 raised:
undefined method `my_selection_index_path' for #<#<Class:0x1197e5676>:0x25439c3b>

第 16 行是上面显示的表单片段.

Line 16 is the form snippet shown above.

我在同一个 Web 应用程序中的所有其他表单/页面都以完全相同的方式设置并且运行良好.但是,一旦我将 erb 表单引用更改为 :my_selection,这个错误就消失了,我的页面表现正常.

All of my other forms/pages in the same web app are set up in exactly the same way and are working fine. However, once I changed the erb form reference to :my_selection, this error went away and my page behaved normally.

问题:

  1. 我对 :my_selections 和 @my_selections 之间区别的理解是否正确?
  2. 为什么切换到 :my_selection 会解决我原来的错误?

推荐答案

我对 :my_selections 和@my_selections 正确吗?

Is my understanding of the difference between :my_selections and @my_selections correct?

不:(

: 表示一个符号,它本质上不是任何东西的别名.它就像一个不可变的字符串,通常用作名称来表示某事.

: indicates a symbol, its not an alias for anything intrinsically. It's like an immutable string, which is often used as a name to represent something.

在 Rails api 接受符号代替实例变量的地方,它在内部实际上是这样做的:

In places where the Rails api accepts a symbol in place of an instance variable, internally it's actually doing this:

self.instance_variable_get "@#{my_symbol}"

实际上返回请求的实例变量的值.

Which actually returns the value of the requested instance variable.

因此,您认为符号完全对应于实例变量的唯一原因是,驱动您正在使用的 API 的代码以这种方式工作.如果没有一个框架来为您做到这一点,那么根本就没有相关性.

So the only reason that you think symbol correspond to instance variable at all, is because the code that drives the API you are using works that way. Without a framework to do that for you, there is no correlation at all.

为什么切换到 :my_selection 会解决我原来的错误?

Why would switching to :my_selection resolve my original error?

for_form(model_instance) 将生成一个表单,如果模型实例未保存,则提交给创建操作,如果模型已经存在于您的数据库中,则提交给更新操作.

for_form(model_instance) will generate a form that submits to the create action if the model instance is unsaved, or to the update action if the model is already exiting in your DB.

不,我不知道@my_selection 中有什么,但无论它是什么类,似乎都没有正确生成路由.

No I don't know what's in @my_selection, but whatever class it is doesn't seem to be generating the routes properly.

resources :my_selections

将生成一个你会像这样调用的路由:

Will generate a route you would invoke like this:

my_selections_path

您的表单如何为 my_selection_index_path 生成路由我不确定,这实际上取决于您的模型是什么.

How your form is generating a route for my_selection_index_path I'm not sure and it really depends on what your models are.

当你传递一个符号,并且没有对应的 ivar 时,它会使用它作为路由生成的模型名称.通过尝试调用 my_selections_path 来做正确的事情,它直接基于您传入的符号.

And when you pass a symbol instead, and there is no corresponding ivar, it uses that as the model name for route generation. Which would do the right thing by trying to invoke my_selections_path, which is directly based on the symbol you pass in.

这篇关于Ruby - :variable 和 @variable 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆