Ruby on Rails development from scratch (Windows) (33)-naming convention

Source: Internet
Author: User
Tags lowercase naming convention require table name ruby on rails

When you first start learning about rails, you might be confused about how to automatically handle naming in rails, such as how to find a table named people in the database based on a model named person, and this time we'll look at the naming conventions in rails.

1. Mixed case, underline, plural

We often use abbreviated named variables, in Ruby, when a convention is a named variable, all letters are lowercase, the middle of the word is separated by underscores, classes (class) and modules are named differently, not underlined, and the initials and initials of the word use uppercase. So we have a class name like Order_status and LineItem in the code we wrote earlier.

Rails uses such naming conventions and expands them. First assume that the table name and variable name in the database are all lowercase and that the middle of the word is separated by an underscore, and that it is all plural, for example: Orders,third_parties. Also, rails assumes that the name of the file also uses lowercase and underscores.

Rails automatically converts names based on these conventions, for example, your program may contain a model class to manipulate line item, and you can use the Rails naming convention to name the class LineItem, and by that name rails will infer the following:

L The table in the database is named Line_items.

l have a line_item.rb file in the App/models directory.

The name of the rails controller (Controller) has a different convention, if you have a store in your program controller,rails will make the following inference:

L There is a class called Storecontroller, and there is a store_controller.rb file under the App/controllers directory.

L in the App/helpers directory, there is a file called Store_helpers, where the class name is called Storehelper.

L Find the view template in the corresponding directory App/views/store of the controller.

L GET the output of the view and convert them to the store.rhtml or Store.rxml layout template in the App/views/layouts directory.

Typically in Ruby's code, we use the Require keyword to introduce classes from some files into the current code, because rails knows the relationship between the filename and the class name, so the Require keyword is not required in the Rails program. When you refer to a class or module that does not know the name, rails converts the class name to the filename according to the naming convention and loads the file, as if you were referencing a model by name, and the model is automatically loaded into the program.

As you can see, this pattern is broken when the class is stored in the session, in which case we explicitly declare them, for example, we are in the controller (Controller):

Class Storecontroller < Applicationcontroller

Model:line_item

Here, the naming convention is still in use, marking: line_item all lowercase and separated by underscores, which causes the line_item.rb file to be loaded, and the file contains class LineItem.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.