• No se han encontrado resultados

5.3. LA PARENTALIDAD POSITIVA

5.3.6. Las competencias profesionales para la promoción de la parentalidad

If you’re still reading this, it means you’re wanting to connect to a database other than MySQL. To do this, you’re going to have to install a database driver. The database libraries are all written in C and are primarily dis- tributed in source form. If you don’t want to go to the bother of building a driver from source, have a careful look on the driver’s web site. Many times you’ll find that the author also distributes binary versions.

If you can’t find a binary version, or if you’d rather build from source anyway, you’ll need a development environment on your machine to build the library. Under Windows, this means having a copy of Visual C++. Under Linux, you’ll need gcc and friends (but these will likely already be installed).

Under OS X, you’ll need to install the developer tools (they come with the operating system, but aren’t installed by default). Once you’ve done that, you’ll also need to fix a minor problem in the Apple version of Ruby (unless you already installed the fix from Lucas Carlson described in the sidebar on the preceding page). Run the following commands.

dave> # You only need these commands under OS X "Tiger"

dave> sudo gem install fixrbconfig

dave> sudo fixrbconfig

1Having said that, if you want to put a high-volume application into production, and

you’re basing it on MySQL, you’ll probably want to install the low-level MySQL interface library anyway, as it offers better performance.

Report erratum

RAILS ANDDATABASES 23

Databases and This Book

All the examples in this book were developed using MySQL (version 4.1.8 or thereabouts). If you want to follow along with our code, it’s probably simplest if you use MySQL too. If you decide to use something different, it won’t be a major problem. You’ll just have to make minor adjustments to the DDL we use to create tables, and you’ll need to use that database’s syntax for some of the SQL we use in queries. (For example, later in the book we’ll use the MySQLnow( ) function to compare a database column against the current date and time. Different databases will use a different name for thenow( ) function.)

The following table lists the available database adapters and gives links to their respective home pages.

DB2 http://raa.ruby-lang.org/project/ruby-db2

MySQL http://www.tmtm.org/en/mysql/ruby

Oracle http://rubyforge.org/projects/ruby-oci8

Postgres http://ruby.scripting.ca/postgres/

SQL Server (see note after table)

SQLite http://rubyforge.org/projects/sqlite-ruby

There is a pure-Ruby version of the Postgres adapter available. Download postgres-prfrom the Ruby-DBI page athttp://rubyforge.org/projects/ruby-dbi.

MySQL and SQLite are also available for download as RubyGems (mysql

andsqliterespectively).

Interfacing to SQL Server requires a little effort. The following is based on a note written by Joey Gibson, who wrote the Rails adapter.

Assuming you used the one-click installer to load Ruby onto your system, you already have most of the libraries you need to connect to SQL Server.

However, the ADO module isnot installed. Follow these steps.

1. Find the directory tree holding your Ruby installation (C:\Ruby by

default). Below it is the folder\Ruby\lib\ruby\site_ruby\1.8\DBD. Inside

this folder, create the directoryADO.

2. Wander over tohttp://ruby-dbi.sourceforge.netand get the latest source

distribution of Ruby-DBI.

3. Unzip the DBI distribution into a local folder. Navigate into this folder,

and then to the directory src\lib\dbd_ado. Copy the file ADO.rb from

Report erratum

KEEPINGUP-TO-DATE 24

this directory into theADOdirectory in the Ruby tree that you created

in step 1.

The SQL Server adapter will work only on Windows systems, as it relies on Win32OLE.

3.5

Keeping Up-to-Date

Assuming you installed Rails using RubyGems, keeping up-to-date is rel- atively easy. Issue the command

dave> gem update rails

and RubyGems will automatically update your Rails installation. The next time you restart your application it will pick up this latest version of Rails. (We have more to say about updating your application in production in the

Deployment and Scalingchapter, starting on page440.)

3.6

Rails and ISPs

If you’re looking to put a Rails application online in a shared hosting envi- ronment, you’ll need to find a Ruby-savvy ISP. Look for one that supports Ruby, has the Ruby database drivers you need, and offers FastCGI and/or lighttpd support. We’ll have more to say about deploying Rails applications

in Chapter22,Deployment and Scaling, on page440.

Now that we have Rails installed, let’s use it. On to the next chapter.

Report erratum

Chapter 4

Instant Gratification

Let’s write a trivial web application to verify we’ve got Rails snugly installed on our machines. Along the way, we’ll get a glimpse of the way Rails applications work.

4.1

Creating a New Application

When you install the Rails framework, you also get a new command-line

tool,rails, which is used to construct each new Rails application that you

write.

Why do we need a tool to do this—why can’t we just hack away in our favorite editor, creating the source for our application from scratch? Well, we could just hack. After all, a Rails application is just Ruby source code. But Rails also does a lot of magic behind the curtain to get our applica- tions to work with a minimum of explicit configuration. To get this magic to work, Rails needs to find all the various components of your applica-

tion. As we’ll see later (in Section13.2,Directory Structure, on page173),

this means that we need to create a specific directory structure, slotting

the code we write into the appropriate places. The railscommand simply

creates this directory structure for us and populates it with some standard Rails code.

To create your first Rails application, pop open a shell window and navi- gate to a place in your filesystem where you’ll want to create your applica- tion’s directory structure. In our example, we’ll be creating our projects in

a directory called work. In that directory, use therails command to create

an application calleddemo. Be slightly careful here—if you have an exist-

ing directory called demo, you will be asked if you want to overwrite any

existing files.

CREATING ANEWAPPLICATION 26

dave> cd work

work> rails demo

create create app/apis create app/controllers create app/helpers : : : create log/development.log create log/test.log work>

The command has created a directory named demo. Pop down into that

directory, and list its contents (using ls on a Unix box or dir under Win-

dows). You should see a bunch of files and subdirectories.

work> cd demo

demo> ls -p

CHANGELOG app/ db/ log/ test/

README components/ doc/ public/ vendor/

Rakefile config/ lib/ script/

All these directories (and the files they contain) can be intimidating to start with, but we can ignore most of them when we start out. For now, we need

to use only one of them, thepublicdirectory.

As its name suggests, thepublicdirectory contains the files that we expose

to our end users, the people using our application. The key files are

the dispatchers: dispatch.cgi, dispatch.fcgi, and dispatch.rb. The dispatch- dispatchers

ers are responsible for accepting incoming requests from users sitting at their browsers and directing those requests to the code in our application. They’re important files, but we won’t need to touch them for now.

You’ll also notice that there’s a script directory underneath demo. It con-

tains some utility scripts that we’ll be using as we develop our applications.

For now, we’ll use the script called server. It starts a stand-alone web

server that can run our newly created Rails application under WEBrick.1

So, without further ado, let’s start the application you just wrote.

demo> ruby script/server

=> Rails application started on http://0.0.0.0:3000 [2005-02-26 09:16:43] INFO WEBrick 1.3.1

[2005-02-26 09:16:43] INFO ruby 1.8.2 (2004-08-24) [powerpc-darwin7.5.0] [2005-02-26 09:16:43] INFO WEBrick::HTTPServer-start: pid=2836 port=3000

As the last line of the start-up tracing indicates, we just started a web

server on port 3000.2 We can access the application by pointing a browser

athttp://localhost:3000. The result is shown in Figure4.1.

1WEBrick is a pure-Ruby web server that is distributed with Ruby 1.8.1 and later. 2The0.0.0.0part of the address means that WEBrick will accept connections on all inter-

faces. On Dave’s OS X system, that means both local interfaces (127.0.0.1 and ::1) and his LAN connection.

Report erratum

HELLO, RAILS! 27

Figure 4.1: Newly Created Rails Application

We’re going to leave WEBrick running in this console window. Later on, as we write application code and run it via our browser, we’ll see this console window tracing the incoming requests. When you’re done using the application, you can press control-C to stop WEBrick.

At this point, we have a new application running, but it has none of our code in it. Let’s rectify this situation.

Documento similar