Problem You have Rails installed on your system and want to create your first Rails project. Solution We'll assume that you have Ruby, RubyGems, Rails, and one of the databases supported by Rails (MySQL is most popular; PostgreSQL is less popular but also an excellent choice). To create a new Rails application, run the rails command with the path to your new application as an argument. For example, to create your new application at /var/www/cookbook (and the cookbook directory doesn't exist yet), type the following command in a terminal window: $ rails /var/www/cookbook The rails command creates the directory for your project using the path you supplied, as well as a number of subdirectories that organize your project's code by the function it performs within the MVC environment. The rails command also accepts several command-line options. You can view these options by typing: $ rails --help The most important of these options is --database=database_type, where database_type is one of the following: mysql, oracle, postgresql, sqlite2, or sqlite3. For example, to use PostgreSQL as your database instead of the default, MySQL, enter the following command: $ rails /var/www/cookbook --database=postgresql Discussion After creating a project with Rails, you should explore the structure of directories it generates, as well as the files that are created. Your new Rails project will include a nice README file that goes over the basics behind Rails, including how to get documentation, debugging Rails, the Rails console, breakpoints, and more. A new Rails project contains the following directories:
app -
Contains all the code that's specific to this particular application. Most of Rails development happens within the app directory.
app/controllers -
Contains controller classes, all of which should inherit ActionController::Base. Each of these files should be named after the model they control followed by _controller.rb (e.g., cookbook_controller.rb) for automatic URL mapping to occur.
app/models -
Holds models that should be named like cookbook.rb. Most of the time model classes inherit from ActiveRecord::Base.
app/views -
Holds the template files for the view that should be named, such as cookbook/index.rhtml for the CookBookController#index action. All views use eRuby syntax. This directory can also be used to keep stylesheets, images, and so on, that can be symlinked to public.
app/helpers -
Holds view helpers that should be named, such as weblog_helper.rb.
app/apis -
Holds API classes for web services.
config -
Contains configuration files for the Rails environment, the routing map, the database, and other dependencies.
components -
Holds self-contained mini applications that can bundle together controllers, models, and views.
db -
Contains the database schema in schema.rb. db/migrate contains all the sequence of migrations for your schema.
lib -
Contains application-specific librariesbasically, any kind of custom code that doesn't belong under controllers, models, or helpers. This directory is in the load path.
public -
The directory available for the web server. Contains subdirectories for images, stylesheets, and Java scripts. Also contains the dispatchers and the default HTML files.
script -
Holds helper scripts for automation and generation.
test -
Contains unit and functional tests along with fixtures.
vendor -
Holds external libraries that the application depends on. Also includes the plug-ins subdirectory. This directory is in the load path. See Also Section 2.2" Section 2.10" |