`

(转)Creating a Ruby Weblog in 10 Minutes

    博客分类:
  • RoR
阅读更多

Creating a Ruby Weblog in 10 Minutes

June 2007 [Revision number: V6.0-2]    

This tutorial describes how to create a blog in less than 10 minutes. This tutorial runs with NetBeans IDE 6.0 (M10) with Ruby support.

Contents

Tutorial Requirements
Setting Up
Creating the Ruby on Rails Project
Configuring the Database
Creating the Model
Using a Scaffold
Migrating Forward
Making the List Look More Like a Blog

Tutorial Requirements

This tutorial requires the following software:

Setting Up

  • Copy the derbyclient.jar (jdk1.6.0_home/db/lib/derbyclient.jar) into your jruby lib directory (NetBeans_install_dir/ruby1/jruby-1.0/lib).

Creating the Ruby on Rails Project

  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 and click Next.
  3. Type RubyWebLog in the Project Name field and click Finish.
  4. Explore the Projects window. As shown in the following figure, the Projects window groups items by category. Expand each of the nodes to see what type goes in each category.

    Figure 1: Projects Window Showing RubyWebLog application

Configuring the Database

  1. In the Projects window, expand the Configuration node.
  2. Open database.yml.
  3. In database.yml, delete the default database configuration under development: and replace it with the following configuration:

    Code Sample 1: Development Database Configuration in database.yml
      adapter: jdbc
      driver: org.apache.derby.jdbc.ClientDriver
      url: jdbc:derby://localhost:1527/sample
      username: app
      password: app

    Make sure that you use the correct format shown in Code Sample 1. If you use tabs, you might encounter an error when you migrate the database.
  4. Open environment.rb and insert the following code above the line that reads Rails::Initializer.run do |config|:

    Code Sample 2: Ruby Code
    if RUBY_PLATFORM =~ /java/
      require 'rubygems'
      RAILS_CONNECTION_ADAPTERS = %w(jdbc)
    end

  5. Open the Services Window, expand the Databases node and check if the Sample {app on APP] database is connected.

    If the jdbc node for the Sample database's badge is broken, the IDE is not connected to the database. To connect to the Sample database, right-click the jdbc node for Sample and choose Connect from the pop-up menu. If the Connect dialog box appears, enter app for the Password, select Remember Password During This Session, and click OK.

Creating the Model

In this section you use the Rails Generator to create a migration. A migration is a way of using versioning to track the changes to your database.

  1. Switch back to 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.

    A migration, 001_create_posts.rb, is created for you as part of the model generation:

    Figure 2: Rails Generator Output

  3. Expand the Database Migrations > migrate node and open 001_create_posts.rb.
  4. Add the title column (shown in bold) to the up method as shown in the following figure:

    Code Sample 3: 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

    The self.up method executes when you run the database migration. The self.down method runs when you stop the database. The self.down method rolls the database back to a previous version.

  5. In the Projects window, right-click the RubyWebLog node and choose Migrate Database > To Current Version.

    This action runs the database migration. The output window indicates when CreatePosts has been migrated. Following are corrections for some common errors:

  6. Switch to the Services window and expand the jdbc > Tables node to see the generated posts table and associated schema_info table.

    If you do not see the new entries, right-click the Tables node and choose Refresh.

    Figure 5: Generated Post Table

Using a Scaffold

In this section, you use the Ruby on Rails scaffold feature. Scaffolding provides the basic interface for creating, editing, viewing, and destroying entries in the blog.

  1. Switch back to 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.
  3. Add the following code, which provides a simple CRUD application around the model, to blog_controller.rb:

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

  4. Under the Configuration node, open routes.rb and add the following code before the final end statement:

    map.connect '', :controller => "blog"
    	
  5. Expand the Public node and delete index.html.

    Now the blog loads automatically when you run the application.
  6. Click the Run Main Project button in the toolbar to start the WEBrick server and launch the browser.

    Following is the first page of the application.

    Listing Posts
  7. Click New post.

    First Entry Post
  8. Enter a title and click Create.

    Following is an example blog listing.

    Listing First Entry

Migrating Forward

Here you change your data model by adding another migration, which adds a body to the editing page.

  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.

    Ruby creates a versioned migration script called 002_add_body.rb.

  2. Modify 002_add_body.rb as follows:

    Code Sample 5: 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
    
  3. Right-click the RubyWebLog node and choose Migrate Database > To Current Version.

  4. Return to the browser and click the Edit link to see how Ruby recognizes the new body field.

    New Body Field

  5. Click Back to return to the Listing Posts page and create a few more blog entries. For example:

    More Blog Entries

Making the List Look More Like a Blog

This far, scaffolding has created a basic CRUD application that enables you to easily test the model. Here you generate scaffolding that you use as a base for modifying your 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. Click OK.
  4. Expand Views > blog and open list.rhtml. Delete the <h1> and <table> tags and replace them with the following code:

    Code Sample 6: 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 %>
  5. Save list.rhtml and refresh your browser.


  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 follows:

评论

相关推荐

    weblog, A simple blog system written in Flask..zip

    《基于Flask构建的简单博客系统——weblog详解》 Flask是一款轻量级的Python Web框架,以其简洁明了的API和灵活的扩展性深受开发者喜爱。在本篇文章中,我们将深入探讨一个名为"weblog"的开源项目,这是一个完全...

    WebLog Expert Lite

    《WebLog Expert Lite:深入解析Web日志分析工具》 WebLog Expert Lite是一款专为WebLogic服务器设计的日志分析工具,旨在帮助用户轻松理解和解析Web服务器产生的日志数据。这款小工具虽然轻量级,但在功能上却丝毫...

    WebLog Expert Lite v7.0

    WebLog Expert是一款专门用来对繁杂的WEB服务器日志文件进行综合分析的软件,可以对你网站的来访者进行详细统计:当前活动会话统计、文件存取统计、搜索使用情况统计、浏览器/操作系统统计、错误统计等。通过HTML...

    Rails-Blog:一个用Ruby on Rails编写的简单Weblog应用程序

    Rails博客这是一个用Ruby on Rails编写的简单Weblog应用程序。这个项目的目的是让我学习一些基本的Web开发原理,以及学习如何使用Ruby语言和Rails框架。它所基于的教程可以在找到。该自述文件通常会记录启动和运行...

    WebLog Expert v4.2

    一款专门用来对繁杂的WEB服务器日志文件进行综合分析的软件,可以对你网站的来访者进行详细统计:当前活动会话统计、文件存取统计、搜索使用情况统计、浏览器/操作系统统计、错误统计等。通过HTML形式的表格和图表...

    WebLog Expert Lite v5.5

    一款专门用来对繁杂的WEB服务器日志文件进行综合分析的软件,可以对你网站的来访者进行详细统计:当前活动会话统计、文件存取统计、搜索使用情况统计、浏览器/操作系统统计、错误统计等。通过HTML形式的表格和图表...

    网站日志分析工具WebLog Expert Lite

    10. **学习与支持**:对于初次使用者,WebLog Expert Lite通常提供详细的用户手册和在线帮助,确保用户能够快速上手。 总之,WebLog Expert Lite作为一款强大的网站日志分析工具,它可以帮助网站管理员深入理解用户...

    WeBlog-Java毕业设计

    WeBlog-Java毕业设计

    weblog_entries.txt

    例如“2012-05-10”和“21:25:44”。这对于时间序列分析非常重要,可以帮助识别访问模式、高峰时段等,从而为业务决策提供依据。 ### IP地址 每个条目的最后一个字段是发起请求的客户端IP地址,例如“148.113.13....

    weblog的部署安装及常见问题

    **WebLogic服务器的部署安装与常见问题** WebLogic Server(简称WebLogic)是Oracle公司的一款企业级Java应用服务器,用于构建、部署和管理基于Java EE(Java Platform, Enterprise Edition)的应用程序。...

    weblog_success.zip

    【标题】"weblog_success.zip" 是一个包含Flask框架构建的个人博客网站的压缩文件。这个项目已经绕过了邮箱验证步骤,意味着用户在注册或登录时可能无需通过电子邮件进行身份验证,这对于快速测试和演示博客系统非常...

    java项目-weblog源码.zip

    javajava项目_weblog源码.zip

    Wordlog: an all ASCII weblog in PHP-开源

    总结来说,Wordlog是一个用PHP编写的开源Weblog系统,依赖于MySQL进行数据存储。其全ASCII的设计使得它在各种环境下都能保持一致性,同时也体现了开源软件的协作精神。通过"wordlog-1.5"压缩包,用户可以下载并部署...

    WebLogic10 配置部署(附图)

    ### WebLogic 10 配置与部署详细指南 #### 一、配置 Domain 在进行 WebLogic 10 的配置之前,首先需要确保已经安装了 BEA 的 WebLogic Server,并且具备基本的操作环境。 1. **启动配置向导**: - 打开“开始”...

    weblog网站日志分析器

    weblog网站日志分析器,可以分析网站的蜘蛛在您网站的爬行记录与网站的收录状态,方便做网站优化的朋友使用!需要.net framework 2.0的支持!

    Home Download Documentation Weblog Community Code

    文档中包含了多个部分:首页(Home)、下载(Download)、文档(Documentation)、博客(Weblog)、社区(Community)和代码(Code)。这些部分共同构成了一个完整的网站结构,使得用户可以方便地获取所需的信息和...

    3.2、weblog集群部署项目session复制1

    【3.2、Weblog集群部署项目Session复制】 在分布式Web应用程序环境中,session复制是确保用户会话在多个服务器之间保持一致的关键技术。在Weblog集群部署中,session复制允许用户在集群中的任意一个服务器上登录或...

    WeBlog-毕业设计资源

    Spring Boot + Vue 3.2 + Vite 4.3 Java ~ Star ~

    WEBLOG v2.1(日记模式个人网站)

    WEBLOG v2.1是一款基于日记模式的个人网站系统,主要目标是为用户提供一个私人空间,以记录并分享他们的日常生活、思想感悟或者专业知识。这款软件的出现,使得个人可以通过网络来构建自己的在线日志,与他人进行...

    pcp-pmda-weblog-5.3.0-3.el8.x86_64.rpm

    官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装

Global site tag (gtag.js) - Google Analytics