官术网_书友最值得收藏!

Navigating the Directory Structure

As we have seen, Rhodes is smart enough to create the elementary folders and files for you. We will now understand the basic directory structure of a Rhodes application.

A Rhodes application has the following directories and files:

  • app – This is the heart of your application, which contains the controllers, models, and views for your application.
  • public – This folder contains all the static files like stylesheet, JavaScript, and images for your application.
  • rhoconfig.txt – This file is used for configuring your application. We will discuss this in detail later in the book.
  • build.yml – This is a very important file for configuration as this contains all the configuration that is needed for building an application. It also contains your application name, version, vendor, etc. It has device SDKs, Rhodes location, and also the extensions that you are using in your application. We will discuss this in detail later in the book.
  • Rakefile – This file is used by Rhodes to execute the inbuilt rake commands.
  • iconThis folder will contain our application icon.

Now that your app is compiled and runs, you can start adding models and controllers. When the model is generated, Rhodes will also create files for a standard User Interface for displaying and editing the model. This follows the standard model-view-controller paradigm.

Time for action – Creating the model

Now, it's time to create the Model, View, and Controller for our employee application. We will create a model employee with attributes such as name, company, address, age, gender, and salary.

$ rhogen model employee name,company,address,age,gender,salary

The model generator will provide us with the following:

     [ADDED]  app/Employee/index.erb
     [ADDED]  app/Employee/edit.erb
     [ADDED]  app/Employee/new.erb
     [ADDED]  app/Employee/show.erb
     [ADDED]  app/Employee/index.bb.erb
     [ADDED]  app/Employee/edit.bb.erb
     [ADDED]  app/Employee/new.bb.erb
     [ADDED]  app/Employee/show.bb.erb
     [ADDED]  app/Employee/employee_controller.rb
     [ADDED]  app/Employee/employee.rb
     [ADDED]  app/test/employee_spec.rb

You will see that RhoGen creates a controller (employee_controller.rb) and several HTML templates (.erb files) for reading and editing objects for the specified model and we have seen the magic of the Rhogen model command. It has generated a lot of code for us. We can divide the generated code into four parts:

  • Model: A model represents the information (data) of the application and the rules to manipulate that data. The file employee.rb is the model for employee application. It contains all the logic.
  • Controller: The controller provides the bond between the model and view. In Rhodes, the controller is responsible for processing incoming requests, interrogating the models for data, and passing that data on to the views for presentation. The file employee_controller.rb is the controller for employee application.
  • View: Views represent the user interface of your application. In Rhodes, views are often HTML files with embedded Ruby code that perform tasks related solely to the presentation of the data. These are index.erb, edit.erb, etc.
  • Spec file: These are the files where we write unit test cases for our code. The file employee_spec.rb is the spec file for employee application.

If you look closely with a single rhogen command, Rhodes has generated piles of files. You must be wondering what the uses for these files and folders are. Well, these files and folders are responsible for basic CRUD operations for your employee model. CRUD operations are CREATE, READ, UPDATE, and DELETE. If you have worked with Ruby on Rails, it is similar to the scaffold command.

What just happened?

We have created an employee model using the Rhogen command. Also, now we are familiar with CRUD operations and the logic behind them.

主站蜘蛛池模板: 通河县| 手游| 望奎县| 灯塔市| 咸丰县| 兴国县| 大英县| 沂源县| 甘孜| 郸城县| 澜沧| 辉县市| 黄骅市| 革吉县| 三原县| 贵溪市| 宁化县| 双辽市| 临汾市| 呼和浩特市| 杂多县| 明光市| 湾仔区| 鄄城县| 维西| 昆山市| 突泉县| 河源市| 长沙县| 洪雅县| 咸丰县| 临湘市| 玛多县| 沅陵县| 古交市| 微博| 清新县| 汕尾市| 子长县| 栾城县| 扶余县|