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

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.

主站蜘蛛池模板: 梁河县| 龙南县| 松潘县| 北安市| 苍南县| 邵东县| 南昌市| 博兴县| 遂宁市| 巴中市| 玉田县| 鹿泉市| 都匀市| 南部县| 衢州市| 体育| 沁水县| 扶风县| 阳春市| 泰来县| 长阳| 墨脱县| 夏津县| 永吉县| 延安市| 民乐县| 茶陵县| 伊宁县| 公安县| 体育| 海阳市| 博乐市| 安义县| 兖州市| 神农架林区| 虹口区| 东方市| 北碚区| 大姚县| 石河子市| 道孚县|