Ruby - Difference between :variable and @variable

孤街醉人 提交于 2020-01-12 03:39:12

问题


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. @ indicates a instance variable (e.g., @my_selection)
  2. : indicates an alias (e.g., :my_selection)

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 snippet

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

route.rb snippet

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>

Line 16 is the form snippet shown above.

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.

Questions:

  1. Is my understanding of the difference between :my_selections and @my_selections correct?
  2. Why would switching to :my_selection resolve my original error?

回答1:


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

Nope :(

: 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.

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.

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.

Why would switching to :my_selection resolve my original error?

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.

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

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.

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.



来源:https://stackoverflow.com/questions/7211724/ruby-difference-between-variable-and-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!