In a previous post I described how to install, create and run Rails. In this article I will discuss some more looks like a standard directory with the application together with the purpose of each folder.
Framework Ruby on Rails fairly accurately determine how you want your each web application. At first glance, some rigid rules may seem a limitation, however, introduced in Rails conventions eventually enable a very quick orientation of each project in Rails, even if you have not worked in a project over which we assume custody. The structure, file naming, and even the names of specific keys in the database schema allows that we can skip the step of defining and setting up at a very accelerated, not only writing, but above all, to work on existing code.
When you open an application, we see more or less a set of directories
app config db
doc lib log
public Rakefile README
script
test tmp vendor
Product app is the most important directory for the project - are placed in the class definitions that make up the implemented in Rails MVC pattern . We find where all controllers, models and views. This directory is divided into sub-folders: controllers - which are in separate files to controllers, models - in which the files are stored definition of the different models. The other folders contain files views and views helpers - the folder that contains the auxiliary modules available during the rendering of the view.
config directory contains the files defining application configuration parameters and above all the settings to access the database config \\ database.yml . In addition to the standard Rails files Ruby (. rb) is often used YAML format (files with the extension. yml). These files are parsed by the Rails environment, and they are read from the configuration parameters.
catalog db contains files associated with the database used - primarily located in the files used to create (and play), so the database schema. migrations. Migrations in Rails creates introduced a special meta-language that allows the manipulation scheme, independently of your database engine - with the migration and they provide mechanisms for abstraction is not to be relevant (except possibly aspect of the extreme performance and data security), or use MS SQL, Postgres or SQLite database files - a diagram will be created in a similar way and obtain the same functionality.
Another important site is the directory log . The standard Rails application can be run in one of three modes: development, test and production. These modes are different configuration settings - including options, which for example determine what is to be recorded in the logs. Each of these modes has its own log file: development.log, test.log, production.log. Preview the files you podpatrzeć eg code models generated by SQL, requests sent by customers of our application with parameters, and the share taken by the controller when generating the response for the browser.
Some applications, which typically consist of files. Css, javascript libraries and static objects flash and pictures used in any web application is not generated by the Rails application. All these files should be stored in the directory public. With these files can be directly used by embedding them in the form of an appeal with links placed in the HTML view.
The directory script includes scripts for managing the application. Most important is the script \\ server - allows you to run the server application. A very important feature is the option-e, which determines the Rails environment starts. If you omit this parameter by default, starts up the development environment (development). Moreover, it is very important script file script \\ generate - it allows the use of the installed along with the Rails code generators - they allow the creation of frameworks of individual application components such as models, controllers, together with views and migration.
The directory test generated code is placed in automated tests. More about the directory structure I posted in the following parts of this tutorial. Now just add that in addition to the tests shall also contains files with test data that are loaded at startup so each test. fixtures.
In the main application directory is also your Rakefile. Rakefile is equivalent to the makefile used to build applications for tools for C / C + +. Role in Rails takes to make the rake of the fact that this is not to compile, and the majority of cases to perform operations such as the database (load test data from the fixtures, the execution of the migration pattern), deletes temporary files, etc. For a complete list of sessions defined operations, we can list all by adding the parameter - tasks like the following
rake - tasks
After discussing the directory structure is time to generate a functional example.
In this series I will try to prepare a simple application for managing contacts. Applications that will create incremental value in a few episodes to show how to apply TDD methodology and BDD.
Assume that our application will initially be allowed to collect contacts. Contact
is an object which will keep the name. To generate such a simple application in Rails, in principle, no need to program. Each project has a Rails generator to generate such a framework, which nevertheless has the functionality to allow you to add, delete and edit a resource, a full CRUD -a. Total generate one command:
ruby \u200b\u200bscript \\ generate scaffold name: string surname: string
The only thing you should do is modify the database schema:
rake db: migrate
and possibly restart the server (close the console and start again)
ruby \u200b\u200bscript \\ server
These commands operate the configuration and database development environment - in other environments, you must set the environment variable RAILS_ENV respectively, testing or production:
rake db: migrate RAILS_ENV = test
ruby \u200b\u200bscript \\ server-e test
Now browser by going to http : / / localhost: 3000/contacts - We can add new contacts and edit or browse existing ones.
In the next post I will try to write the first tests of this application and to modify slightly the initial functionality.