使用NeatBeans十分钟开发一个ruby on rails 的项目

这个教程是neatbeans上边的。

学习的时候注意一下数据库的配置:

a.将mysql里边 my.ini里边的默认语言设置改成 utf8。

b.在项目的数据库配置文件里边,也就是database.yml文件里边增加一个字符编码的设定。如下:

  1. development:   
  2.   adapter: mysql   
  3.   database: test   
  4.   username: root   
  5.   password: root   
  6.   host: localhost   
  7.   encoding: utf8  

 

Creating a Ruby Weblog in 10 Minutes

In this tutorial, you use the Ruby support in the NetBeans IDE to create and run a simple web application. The example shows how to create a Ruby web log. You follow the basic workflow of creating the model, adding a controller, and creating a view.

Contents

Tutorial Requirements
Creating the Sample Database
Creating the Ruby on Rails Project
Configuring the Database Environment
Creating the Model
Migrating the Database
Creating a Controller
Running the Application
Doing More: Adding Another Field
Doing More: Making the List Look More Like a Blog
 

Tutorial Requirements

This tutorial requires the following technologies and resources:

Creating the Sample Database

Note: This tutorial uses the MySQL database server. See Installing and Configuring Ruby Support for information about using a MySQL database server in a Ruby application. This document also contains tips on how to use the Java DB database server instead.

Before you create the Ruby on Rails project, create a rubyweblog_development database, as described below.

  1. Open a command window.
  2. If it has not already been started, start the MySQL database server.
  3. Type the following command to create the development database and press Enter.
    mysqladmin -u root -p create rubyweblog_development
    Note: If the root user does not have a required password, omit the -p argument.

Creating the Ruby on Rails Project

You begin by creating a Ruby on Rails project. By default, the application is created in a directory structure that conforms to the Ruby on Rails project conventions for applications.
  1. In the NetBeans IDE, choose File > New Project.
  2. Select Ruby in the Categories field and Ruby on Rails Application in the Projects field. Click Next.

    Note: The first time that you create a Ruby project in the IDE, the IDE checks to see if you have any other Ruby installations in addition to the bundled JRuby software. If you do, the IDE displays a dialog box asking you to select which software to use. Choose JRuby if you want to use the bundled JRuby interpreter, or choose your Ruby installation if you prefer to use it instead. For more information, see Configuring the IDE to Use Your Own Ruby Installation in the Installing and Configuring Ruby tutorial.

  3. Type RubyWebLog in the Project Name field. Accept all the other default settings.
  4. Click Finish to create the new project.

    The IDE creates the project directory with the same name as your project. You see:

    • The basic categories of the application in the Projects window. Of particular interest are the Controllers, Models, and Views nodes. In this tutorial, you follow the basic workflow of creating the model, adding a controller, and creating a view.
    • A list of files that are part of the application in the Output window. You can click a link in the Output window to open a file in the editing area.

Configuring the Database Environment

The next step is to edit the file database.yml, which is already configured to use the MySQL adapter and the development database. You do not need to do any configuration unless the root user requires a password.
  1. Open Configuration > database.yml
  2. Edit the database.yml by providing the password in the development configuration.
  3. Save and close the database.yml file.

    Important Note: If your operating system's host file does not contain localhost, use 127.0.0.1 instead. Note also that with some systems, the database setting must be in lower case letters.

Creating the Model

Here you use the Rails Generator to create a model for the application. The RubyWebLog application requires a Post model for storing instances of blog posts.

  1. In the Projects window, right-click the Models node, and choose Generate.
  2. In the Rails Generator dialog box, type Post in the Arguments field and click OK.

    The Rails Generator creates a model named Post. The Output window lists that files that are created as part of the model generation:

    • app/models/post.rb. A file that holds the methods for the Post model. This file is also opened in the editing area.
    • test/unit/post_test.rb. A unit test for checking the Post model.
    • test/fixtures/posts.yml. A test fixture for populating the model.
    • db/migrate/migrate/001_create_posts.rb. A migration file for defining the initial structure of the database.

Migrating the Database

The file that you work with next is the migration file, 001_create_posts.rb. You add information to configure the database.
  1. In the Output window, click the link for the 001_create_posts.rb file.

    The file opens to show the self.up method, which creates a table called posts, and the self.down method, which tears the posts table down.

  2. Add the title column (shown in bold) to create_table in the self.up method as shown in the following figure:

    Code Sample 1: Code for 001_create_posts.rb
    class CreatePosts < ActiveRecord::Migration
      def self.up
        create_table :posts do |t|
          t.column "title", :string
        end
      end
    
      def self.down
        drop_table :posts
      end
    end

  3. From the main menu, choose File > Save All.
  4. In the Projects window, right-click the RubyWebLog node and choose Migrate Database > To Current Version.

    This action updates the the database to include the posts table. The Output window indicates when the migration is complete.

Creating a Controller

Now you use the Rails Generator to create a controller to interact with the Post model. In this tutorial, you add scaffolding code, which provides a simple CRUD interface for creating, reading, updating, and deleting entries in the blog.
  1. In the Projects window, right-click the Controllers node and choose Generate.

  2. In the Rails Generator dialog box, type Blog in the Name field. Leave the Views field blank. Click OK.

    This action creates the file blog_controller.rb and opens the file in the editing area. A blog_controller.rb node is added under the Controllers node in the Projects window.
  3. Edit blog_controller.rb by adding the following scaffolding code, which provides a simple CRUD application around the Post model.

    Code Sample 2: Code for blog_controller.rb
    class BlogController < ApplicationController
      scaffold :post
    end

Running the Application

Now test the application.
  1. Under the Configuration node, open routes.rb. Find the following line:

    # map.connect '', :controller => "welcome"
    	
  2. Edit the line by removing the comment sign (#) and changing welcome to blog.
  3. Expand the Public node, right-click index.html and choose Delete.

    index.html displays a default Welcome page, which is not what you want. By deleting index.html, Rails looks in routes.rb to figure out what page to display, which you set to the blog in the previous step.

  4. Choose File > Save All.
  5. Click the Run Main Project button in the toolbar.

    This action starts the WEBrick server, which is part of the Ruby on Rails framework, and launches the web browser. Following is the first page of the application.

    Figure 1: RubyWebLog Home Page


  6. Click New post.

    Figure 2: Page for Creating a New Post


  7. Enter a title and click Create.

    Following is a sample blog post.

    Figure 3: Successful Creation of Blog Post

Doing More: Adding Another Field

Here you add another field so that, in addition to the Title field, the posts table includes a Body column for providing the text of the blog. The steps for creating a field should be familiar by now.
  1. Right-click the Database Migrations node and choose Generate. In the Rails Generator dialog box, type AddBody in the Arguments field and click OK.

    The IDE creates and the versioned migration script 002_add_body.rb and opens the file in the editing area.

  2. Modify 002_add_body.rb as follows:

    Code Sample 3: Code for 002_add_body.rb
    class AddBody < ActiveRecord::Migration
      def self.up
        add_column 'posts', 'body', :text
      end
    
      def self.down
        remove_column 'posts', :body
      end
    end
    

    This migration adds a body column to the posts table and removes it again, if you are backing out of the migration.
  3. Choose File > Save All.
  4. Right-click the RubyWebLog node and choose Migrate Database > To Current Version.
  5. Return to the browser and click the New Post link to see how Ruby recognizes the new body field.

    Figure 4: New Post With Body Field

  6. Create a few more blog entries. For example:

    Figure 5: More Blog Posts

Doing More: Making the List Look More Like a Blog

This far, the scaffold method used in the BlogController created a basic CRUD application that enabled you to easily test the Post model. Now, you generate the same views used by the scaffold method so you can customize the user interface.
  1. In the Projects window, right-click the Views node and choose Generate.
  2. In the Rails Generator dialog box, choose scaffold from the Generate drop-down list.
  3. Type Post in the Model Name field and Blog in the Controller Name field. Leave the Actions field blank. Select Overwrite to force the BlogController to be regenerated, and then click OK.

    The IDE creates a view for the Post model and lists the contents in the Output window.
  4. Expand Views > blog and open list.rhtml, which is used to show the list of blog entries. Delete the <h1> and <table> tags and replace them with the following code:

    Code Sample 4: Code for list.rhtml
    <h1>The Ruby Blog</h1>
    
    <% @posts.each do |post| %>
      <h2><%= post.title %></h2>
      <p><%= post.body %></p>
      <small> <%= link_to 'Permalink', :action => 'show', :id => post %></small>
      <hr>
    <% end %>

    For each instance of a post action, this code produces a a title, body and Permalink, as shown in Figure 6.

  5. Choose File > Save All and then refresh your browser to see the new interface for the Post model.

    Figure 6: New and Improved Model Interface

  6. To display the blog with the most recent entry first, reverse the sort order by adding .reverse to the end of @posts in list.rhtml:

    	  <% @posts.reverse.each do |post| %>
    	  

    When you save the file and refresh your browser, the blog displays as shown in the following figure:

    Figure 7: Blog Posts in Reverse Order

ruby 代码

你可能感兴趣的:(on,Ruby,Ruby,Rails,project,NeatBeans)