If I generate a scaffold called "product"
, what are the differences between the following when I use them in coding my app? @products
, @product
, @Product
, @Products
, Products
, Product
, product
, and products
(I'm pretty sure those aren't all used, but it should at least give the idea of what I'm referring to).
I can't seem to find a simple explanation of what each combination of @
/capitalization/plurality means. Help would be greatly appreciated.
You can read Ruby style guide and Rails style guide, and you get answer on your question.
The only reserved word is capitalized Product
and this references the object model. For example to get all products and save it in the variable @products
you would do:
@products = Product.all
@product = Product.find(1)
If you left off the @
sign off of 'products' then it would be saved only as a local variable and your view would not be able to access it.
In this example @products
and @product
are variables you declare in the controller. You could just use:
@lotsofproducts = Product.all
For Ruby naming conventions, you can check here: http://rubylearning.com/satishtalim/ruby_names.html
As for Rails' naming conventions, it's linguistic: Model - singular, Controller - plural, DatabaseTable - plural(snake_case), View(directory name) - plural. You can check from http://guides.rubyonrails.org/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With