Creating a ruby weblog in 10 minutes

Source: Internet
Author: User
Tags ruby on rails netbeans

Contributed by Brian Leonard, maintained by Gail Chappell
December 2007
[Revision number: V6.0-6]

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
- Locking ing the database environment
- Creating the Model
- Migrating the database
- Creating a controller
- Running the application
- Doing more: adding another field
- Doing more: validating input
- Doing more: making the list look more like a blog
 

Tutorial requirements

This tutorial requires the following technologies and resources:

  • A Database Server
  • Netbeans ide 6.0 with Ruby support

Note:This tutorial, which is written for rails 1.2.5, involves scaffolding. As of rails 2.0, scaffolding is no longer supported.

Creating the sample database

Note:This tutorial uses the MySQL database server. see the installing and processing ing Ruby support article for information about using a MySQL database server in a ruby application. the article also describes 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-PArgument.

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 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 installing ing the IDE to use your own Ruby installation in the installing and inserting ing Ruby tutorial.

  3. TyperubyweblogIn 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 following:

    • The basic categories of the application in the projects window. of participating 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.
    • The database.ymlFile in the editing area.

Locking ing the database environmentThe 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. In the editing area, editdatabase.ymlBy providing the password in the Development configuration.
  2. Save and closedatabase.ymlFile.

    Note:If your operating system's host file does not containLocalhost, Use127.0.0.1Instead.

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, typePost title:stringIn the arguments field and click OK.

    The rails generator creates a model named post. The output window lists the 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/0020.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.
  1. In the output window, click the link for001_create_posts.rbFile.

    The file opens to showself.upMethod, which creates a table called posts, andself.downMethod, which tears the posts Table down, as shown in the following code sample:

    Code sample 1: code001_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

  2. In the projects window, right-click the rubyweblog node and choose migrate database> to current version.

    This action updates 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, typeBlogIn the Name field. Leave the views field blank. Click OK.

    This action creates the fileblog_controller.rbAnd opens the file in the editing area. A blog_controller.rb node is added under the controllers node in the projects window.

  3. Editblog_controller.rbBy adding the following scaffolding code, which provides a simple crud application around the post model:

    Code sample 2: codeblog_controller.rb
    class BlogController < ApplicationController
    scaffold :post
    end

Running the applicationnow test the application.
  1. Under the configuration node, openroutes.rb. Find the line:

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

    index.htmlDisplays a default welcome page, which is not what you want. By deleting index.html, Rails looks inroutes.rbTo 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 the new post link to display the second page of the application, shown below.

    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 should des a body column for providing the text of the blog. the steps for creating a field shoshould be familiar by now.
  1. Right-click the database migrations node and choose generate. In the rails generator dialog box, typeAddBodyIn the arguments field and click OK.

    The IDE creates the versioned migration script002_add_body.rbAnd opens the file in the editing area.

  2. Open up a line underDef self. Up, TypeMcol, And press tab.

    The IDE replacesMcolTrigger with the following code template with 3 parameters:

    add_column :table, :column, :string
  3. TypePostsTo replace the first parameter, then press TAB. TypeBodyAnd press tab again. Then typeTextTo replace the third parameter. The line shoshould look like the following statement:

    add_column :posts, :body, :text

    This migration adds a body column to the posts table.

  4. Choose File> Save all.
  5. Right-click the rubyweblog node and choose migrate database> to current version.

    Alternatively, right-click in the source file and choose run from the pop-up menu.

  6. Return to the browser and click the new post link to see how Ruby recognizes the new body field, shown in the following figure.

    Figure 4: New post with body Field

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

    Figure 5: More blog posts

Doing more: validating input

Here, you add code to the post class to ensure that the users provide values for both the title and the body fields.
  1. In the projects window, expand the models node and double-click post. RB to open the file in the editor.
  2. Open up a line inside the class definition, TypeVP, Then press tab.

    The IDE replacesVPTrigger with the following code template:

    validates_presence_of :attribute
  3. TypeTitle,: Body. The Code shocould look like the following statement:
    validates_presence_of :title, :body
  4. Run the application, click New post, and click Create.

    The application now reports that the title and body cannot be blank.

Doing more: making the list look more like a blogso 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 that 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. TypePostIn the model name field andBlogIn 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 openlist.rhtml,Which is used to show the list of blog entries. Delete the

    Code sample 4: codelist.rhtml

    <% @posts.each do |post| %>
    <p><%= post.body %></p>
    <small> <%= link_to 'Permalink', :action => 'show', :id => post %></small>
    <% end %>

    For each instance ofpostAction, this code produces a title, body, and permalink, as shown in figure 6.

    Tip: Liai(Link with an action and index) is the trigger for the following code template:

    <%= link_to "link text...", :action => "edit", :id => @item %>
  5. Run the application 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 inlist.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


Next steps

  • To continue with the ruby web log tutorial and learn more about Ruby support in the netbeans IDE, go to building relationships between rails models.

  • To obtain support and stay informed of the latest changes to the netbeans Ruby development features, join

    Users@ruby.netbeans.org
    & #117; & #115; & #101; & #114; & #115; & # X40; & #114; & #117; & #98; & #121; & #46; & #110; & #101; & #116; & #98; & #101; & #97; & #110; & #115; & #46; & #111; & #114; & #103;

    And

    Dev@ruby.netbeans.org
    & #100; & #101; & #118; & # X40; & #114; & #117; & #98; & #121; & #46; & #110; & #101; & #116; & #98; & #101; & #97; & #110; & #115; & #46; & #111; & #114; & #103;

    Mailing lists.

  • To submit your own netbeans Ruby tutorial, visit the netbeans community docs page.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.