This is a translated page. The original can be found here: http://www.hoshi.it/blog/index.php/2008/06/08/rails-step-2-hello-world/
giu 08 June 08

Se avete seguito per bene lo Step 1 dovreste avere un ambiente di sviluppo sufficientemente configurato per cominciare a sviluppare applicazioni web più o meno complesse tramite Ruby on Rails. If you have followed all right Step 1 you should have a development environment set up sufficiently to start developing web applications of varying complexity using Ruby on Rails. In questo articolo analizzeremo gli elementi che compongono una applicazione Rails e costruiremo il nostro primo programma: il classico “Hello World!”. In this article we will analyze the elements that make up a Rails application and build our first program: the classic "Hello World".

Creare una applicazione è molto semplice: aprite un prompt DOS (Start -> Esegui… -> “cmd”), spostatevi nella directory dove desiderate immagazzinare l'applicazione e digitate il seguente comando: “ ruby rails hoshi “. Create an application is very simple: open a DOS prompt (Start -> Run ... -> "cmd"), to the directory where you want to store the application and issue the following command: "ruby rails hoshi". Il comando “ruby rails” perciò permette di creare lo scheletro dell'applicazione. The command "ruby rails" so you can create a skeleton application. “hoshi”, se non lo avete capito, è il nome che abbiamo dato all'applicazione. "Hoshi", if you have not understood, is the name we gave to the application.

Se non sono apparsi messaggi di errore, dovreste vedere nella schermata del DOS una lista di cartelle. If no error messages appeared, you should see a DOS screen a list of folders. Analizziamole tutte una per una: Examine them all one by one:

  • /app: è qui che inseriremo il codice sorgente dell'applicazione che comporrà i Controller, i Models, le View e gli Helper. / App: this is where we enter the source code that will form the Controller, Models, and the View Helper. I Controller si occupano di ricevere le richieste dal client, interagire con gli oggetti tramite i Models e inviare risposte al client tramite le View; i Models sono appunto gli oggetti che contengono dati da o verso una struttura dati persistente (in genere un database); infine le View sono fisicamente le pagine HTML (o meglio RHTML in Rails) che saranno richiamate dal Controller per la visualizzazione a video. The controller involved in receiving requests from clients, interacting with objects via Models and send replies to the client via the View; Models are precisely the objects that contain data to or from a persistent data structure (typically a database); Finally the View are physically HTML pages (or better RHTML in Rails) which will be called by the Controller for the video display.
  • /components: questa cartella può contenere componenti che possono essere riutilizzati in altri progetti futuri. / Components: This folder may contain components that can be reused in future projects.
  • /config: questa cartella conterrà le (poche) configurazioni necessarie per l'esecuzione dell'applicativo. / Config: This directory will contain the (few) configurations necessary to run the application.
  • /db: conterrà informazioni sul database e sulle migration (ossia modifiche dinamiche del database senza intaccare troppo nella sua configurazione). / Db: the database and will contain information on migration (ie dynamic changes of the database without affecting too much in its configuration).
  • /doc: directory per la documentazione. / Doc: documentation directory.
  • /lib: directory per importare librerie esterne non presenti tra le librerie standard di Ruby. / Lib: directory to import external libraries that are not in the standard library of Ruby.
  • /log: contiene i file di log generati dal Web Server / Log: contains log files generated by the Web Server
  • /public: sarà inserito tuttociò che riguarda la visualizzazione ma che non ha niente a che fare con il regolare flusso del programma. / Public: will be inserted tuttociò covering the screen, but that has nothing to do with the regular flow of the program. Per esempio: layout, fogli di stile e javascript. For example, layout, style sheets and javascript. Tuttociò che insomma ha a che fare con lo stile dell'applicazione e non del suo processo dati. Tuttociò fact that has to do with the style of his application and process data.
  • /script: può contenere particolari script di gestione dell'applicazione. / Script: script may include specific management application.
  • /test: cartella per i test. / Test: folder for testing.
  • /tmp: directory per i file temporanei (session, cookie ecc..). / Tmp directory for temporary files (session, cookies, etc. ..).
  • /vendor: directory per eventuali plugin che vogliamo installare per la nostra applicazione. / Vendor: directory for any plugins that we install for our application.

Tutti questi nomi e cartelle non vi devono spaventare: servono solo a dare una paronamica generale. All these names and folders there should scare: only serve to give a general paronamica. Adesso non suggeriscono nulla ma quando impareremo a creare applicazioni sempre più complesse sarà tutto più semplice. Now do not suggest anything, but when we learn to build increasingly complex applications will be much easier.

Per il nostro “Hello World” ad esempio sarà necessario solamente creare un controller e una view. For our "Hello World" example will only need to create a controller and a view.

Ecco come: se avete lasciato aperto la finestra DOS, entrate nella cartella del progetto appena creato (digitando “cd hoshi”), digitate il seguente comando “ ruby script/generate controller hello “. Here's how: If you have left open the DOS window, enter the project folder you just created (by typing "cd hoshi"), type the following command "ruby script / generate controller hello". Se esaminate il contenuto delle cartelle hoshi/app/controllers e hoshi/app/views noterete la presenza rispettivamente di un “hello_controller.rb” e di una cartella chiamata “hello”. If you examine the contents of folders hoshi / app / controllers and hoshi / app / views, respectively, you will notice the presence of a "hello_controller.rb" and a folder called "hello". Il primo si occuperà di accogliere la richiesta dell'utente mentre la seconda conterrà la pagina RHTML da mostrare. The first will accommodate your request and the second will contain the page RHTML to display.

Adesso non ci rimane altro che creare la pagina index.rhtml all'interno della cartella “hello” in hoshi/app. Now there remains nothing more than create the page index.rhtml inside the folder "hello" in hoshi / app. Il codice sarà semplicissimo: nel file scrivete “Hello World! The code is simple: the file type "Hello World! Hoshi.it”. Hoshi.it ". Naturalmente potete metterci tutto l'HTML e codice Ruby che volete ma per ora vogliamo proprio iniziare da zero. Of course you can put all the HTML and Ruby code you want but for now we just start from scratch. Salvate il file e tornate alla finestra DOS. Save the file and return to the DOS window. Sempre stando all'interno della directory principale hoshi, digitate il comando “ ruby script/server ” ed avviare così WEBRick (il Web Server che ci permetterà di testare l'applicazione). And according hoshi in the main directory, type the command "ruby script / server" and to launch WEBrick (the Web server that will allow us to test the application).

Aprite il browser e nella barra degli indirizzi digitate “ http://localhost:3000/hello “. Open your browser and type in the address bar "http://localhost:3000/hello". Se non avete fatto errori, dovreste vedere una cosa del genere If you have not made mistakes, you should see something like

Free Image Hosting at www.ImageShack.us

Translate this post Translate this post

Articoli simili Similar Articles

  • Nessun articolo simile trovato No items found similar

Lascia la tua risposta Leave your answer