I understand that symbols and strings are not same, but I don't understand the concept of symbols.
As I understand, a symbol refers to something else. I use the word refer here for my better understanding, not in the in sense of C/C++ pointers or references.
When I create a new controller in Rails, using something like
rails g controller Posts
, does Ruby automatically create a new symbol:posts
, which refers to thePosts
controller? Is this:posts
symbol globally accessed from anywhere in the application? Only in that case, I can understand a construct like:resources :posts
which Ruby can translate for itself: "Ok, I know that :posts
refers to posts_controller
, so I need to create CRUD routes for that controller". Am I right?
What roles do symbols
:title
and:content
have in this example? I assume thatform.label
is equivalent toform.label(:title)
.<%= form_for @post, :html => {:class => "new_post"} do |form| %> <%= form.label :title %><br> <%= form.text_field :title %><br> <%= form.label :content %><br> <%= form.text_area :content %><br> <%= form.submit("Add new post") %>
Are there built-in symbols like
:all
and symbols that are created on-the-way? When I say on-the-way, as I can see when I create some method like:def something ... end
Does Ruby create a new symbol
:something
, which will refer to methodsomething
? If that is correct, why do I need that symbol, where can I can use it?@posts = Post.all #Or Post.all() => Standard OOP approach, i know what happens here @posts1 = Post.find(:all) # Dont know what happens here
What about
@posts1
, what am I doing here? What do:all
mean here? Is it some constant value (not string) for Ruby to know that it must pull everyPost
from DB?