Ruby / Rails naming conventions

If I create a scaffold called "product" , what are the differences between them when I use them when coding my application? @products , @product , @product , @products , Products , Product , Product and Products (I'm sure that they are not all used, but this should at least give an idea of ​​what I mean).

I can't seem to find a simple explanation of what each @ / capitalization / multiple combination means. Help would be greatly appreciated.

+4
source share
3 answers

The only reserved word is capitalized Product , and this refers to the object model. For example, to get all the products and store them in the @products variable, you would do:

 @products = Product.all @product = Product.find(1) 

If you stop the @ "products" sign, it will only be saved as a local variable, and your view will not be able to access it.

In this example, @products and @product are the variables that you declare in the controller. You can simply use:

 @lotsofproducts = Product.all 
+5
source

You can read the Ruby Style Guide and the Rails Style Guide , and you will get the answer to your question.

+8
source

For Ruby naming conventions, you can check here: http://rubylearning.com/satishtalim/ruby_names.html

As for Rails naming conventions, it is linguistic: Model - single, Controller - plural, DatabaseTable - plural (snake_case), View (directory name) - plural. You can check out http://guides.rubyonrails.org/

+3
source

Source: https://habr.com/ru/post/1446310/


All Articles