Creating a Ruby Weblog in Minutes

Source: Internet
Author: User
Tags class definition generator ruby on rails netbeans




Contributed by Brian Leonard, maintained by Gail Chappell

December 2007 [Revision Number:v6.0-6]






In this tutorial, 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 of 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: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 are 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. The installing and configuring Ruby Support article for information about using a MySQL database server in a ruby appl Ication. The article also describes the Java DB database server instead.






Before create the Ruby on Rails project, create a rubyweblog_development database, as described below. Open a command window. If It has not already been started, start the MySQL database server. 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 is not have a required password, omit the- p argument. creating the Ruby on Rails ProjectYou begin by creating a Ruby on Rails project. By default, the application are created in a directory structure that conforms to the Ruby on Rails project conventions for Applications. In the NetBeans IDE, choose File > New Project.



Select Ruby in the Categories field and Ruby on Rails application in the Projects field. Click Next. Note: The "the" the "You create a" Ruby project in the IDE, the IDE checks if your have any other Ruby installations in Addit Ion to the bundled JRuby software. If you do, the IDE displays a dialog box asking your to select which software to use. Choose JRuby If you are want to use the bundled JRuby interpreter, or Choose your Ruby installation if your prefer to use it in Stead. For more information, you'll configuring the IDE to use Your Own Ruby installation in the installing and configuring Ruby Tut Orial. Type Rubyweblog in the Project Name field. Accept all of the other default settings.






Click Finish to create the new project.






The IDE creates the project directory with the same name as your project. You are following: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. Can click a link in the Output window to open a file in the editing area. The Database.yml file in the editing area. Configuring the Database environment the next step are to edit the file database.yml, which is ALR Eady configured to use the MySQL adapter and the development database. You don't need to does any configuration unless the root user requires a password. In the editing area, edit the DATABASE.YML by providing the password in the development configuration.






Save and close the Database.yml file. Note: If your operating system ' s host file does not contain localhost, use 127.0.0.1 instead. Creating the Model






Here, use the Rails generator to create a model for the application. The Rubyweblog application requires a Post model for storing instances of blog posts. In the Projects window, right-click the Models node and choose Generate.






In the Rails Generator dialog box, type Post title:string in the Arguments field and click OK.






The Rails generator creates a model named Post. The Output window lists the "files" 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, work with next is the migration file, 001_create_posts.rb.






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 P OSTs table down, as shown in the following code sample:




Code Sample 1:code for 001_create_posts.rb

Class Createposts < Activerecord::migration

 def self.up

   Create_table:p OSTs do |t|

     T.column:title,: String

   End

 End


 def Self.down

   Drop_table:p OSTs

 End

End


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 the migration is complete. Creating a Controller now for the Rails generator to create a Controller to interact with the Post Model. In this tutorial, your add scaffolding code, which provides a simple CRUD interface for creating, reading, updating, and de leting entries in the blog.






In the Projects window, right-click the Controllers node and choose Generate.






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 to the editing area. A blog_controller.rb node is added under the Controllers node in the Projects window.






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:p OST End

Running the applicationNow test the application.



Under the Configuration node, open routes.rb. Find the line:




# map.connect ',: Controller => "Welcome"


Edit the line by removing the comment sign (#) and changing Welcome to blog.



Expand The public node, right-click Index.html and choose Delete.






index.html displays a default Welcome page, which is isn't what you want. By deleting index.html, Rails looks into routes.rb to figure out what page to display, which your set to the blog in the Prev IOUs step. Choose File > Save all.






Click the Run Main Project button in the toolbar.






This action starts the Webrick server, which are part of the Ruby on Rails framework, and launches the Web browser. Following is the the application.






Figure 1:rubyweblog Home Page






Click the New post link to display the second page of the application, shown below.






Figure 2:page for creating a New Post






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 is where you add Another field, and in addition to the Title field, the P OSTs table includes a body column for providing the text of the blog. The steps for creating a field should is familiar by now.






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 the versioned migration script 002_add_body.rb and opens the file in the editing area.






Open up a line under Def self.up, type Mcol, and press Tab.






The IDE replaces the Mcol trigger with the following code template with 3 parameters:




Add_column:table,: column,: string


The

Type posts to replace the-parameter, then press TAB. Type body and press TAB again. Then type text to replace the third parameter. The line should look like the following statement:


add_column:p osts,: Body,: Text

This migration adds a Bo DY column to the posts table. Choose File > Save all.

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.


The browser and click the new Post link to the "How Ruby recognizes" the new body field, shown in the Follo Wing figure. The


Figure 4:new Post with the body Field


Create a few the more blog entries. For example:




Figure 5:more Blog Posts doing more:validating Input Here's the add code to the Post class to ensure that the users provide values for both the title and the body fields. In the Projects window, expand the Models node and double-click post.rb to open the file in the editor.






Open up a line inside the Class definition, type VP, then press TAB. The IDE replaces VP trigger with the following code template:




Validates_presence_of:attribute


Type title,: Body. The code should look like the following statement:

Validates_presence_of:title,: Body




Run the application, click New Post, and click Create. The application now reports is blank the title and body cannot. doing more:making the List look more like a Blog So far, the scaffold method used in the BlogController created a basic CRUD application which is enabled to easily test th e Post model. Now, you generate the "same views used by the scaffold" so "you can customize the user interface." In the Projects window, right-click the Views node and choose Generate. In the Rails Generator dialog box, choose Scaffold from the Generate drop-down list.






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.






Expand views > blogs and open list.rhtml, which is used to show the list of blogs entries. Delete the


Code Sample 4:code for list.rhtml





For each instance of the A post action, this is code produces a title, body, and permalink, as shown in Figure 6. Tip: liai (link with a action and index) is the trigger for the following code template:




<%= link_to "link text ...",: Action => "edit": ID => @item%>




Run the application to the new interface for the Post model.






Figure 6:new and improved Model Interface






To display the blog with the most recent entry-a-reverse the sort order by adding. Reverse to the "end of" @posts in Li St.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 about Ruby support in the NetBeans IDE, go to building Relations Hips Between Rails Models.






To obtain support and stay informed of the latest changes to the NetBeans Ruby development features, join the Users@rub Y.netbeans.org and dev@ruby.netbeans.org 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.