J2EE

Java/J2EE Struts Spring

常用链接

统计

最新评论

ROR 学习笔记系列二 基于ROR的简单Hello World程序入门

     本篇是本系列的第二个教程,在本章里面我将就开发一个简单的ROR Hello World程序帮助大家入门,任何程序入门都会使用Hello World!这个程序设计里面的经典,使用率最高,几乎每个程序设计人员都写过.当然我个人写程序最入门的我也一般是先写一个Hello World程序来进行入门...好了,下面我们开始吧!

   首先,我们要确定自己的工作目录,比如我的工作目录就是E:\RailsDemo下,这个就是我现在的临时工作目录了.然后在下面创建项目,使用rails会自动帮我们创建项目:

使用

rails HelloWorld -d mysql
或者
rails -d mysql HelloWorld
都可以创建一个HelloWorld项目在RailsDemo目录下
image
然后进入我们的项目中:
cd HelloWorld
修改config/database.yml 为下面的内容,(其中数据库在上一讲中我们已经创建好了,没有创建的话可以进入mysql命令行中使用create database WebServer进行创建.同时密码修改为自己的mysql数据库密码.记得所有的这些属性后面的:后面都要加入一个空格.否则会产生错误.)
development:
  adapter: mysql
  encoding: utf8
  database: WebServer_development
  pool: 5
  username: root
  password: 123123
  host: localhost
 
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: mysql
  encoding: utf8
  database: WebServer_development
  pool: 5
  username: root
  password: 123123
  host: localhost
 
production:
  adapter: mysql
  encoding: utf8
  database: WebServer_development
  pool: 5
  username: root
  password: 123123
  host: localhost
然后使用Rails命令创建控制器HelloWorld.使用命令:
ruby script/generate controller helloworld index
这个命令会给你创建一个controller在app/controller下,还会在app/views下创建一个helloworld文件夹并且在文件夹下创建一个index.html.erb文件.在app/helpers下和test下也创建了一些东西.但是现在我们值关心在controller和views下创建的东西.好的下面让我们来到controller下进行修改helloworld_controller.rb;
修改后内容如下:
class HelloworldController < ApplicationController
  def index
    @sayhello = "Hello World at "+Time.now.to_s 
  end
 
end
修改views/helloworld/index.html.erb文件内容如下:
<h1>Helloworld#index</h1>
<p>Find me in app/views/helloworld/index.html.erb</p>
<%=@sayhello%>
我们只添加了最后一句话,上面所有都是rails自动生成的.
然后创建Mongrel服务:
image
mongrel_rails service::install -N WebServer -c E:\RailsDemo\HelloWorld -e development
在Windows服务下我们会发现一个WebServer服务,启动WebServer服务
image
然后打开http://localhost:3000/ 我们可以看到当前的服务器配置是否成功.
image
然后使用http://localhost:3000/helloworld 进行查看我们可以看到
image
由于Ruby是解释性的语言,所以我们可以直接修改后不用重启服务器,只需要刷新一下就可以查看新的效果了.我们修改controller中的sayhello:
class HelloworldController < ApplicationController
  def index
    @sayhello = "hahahahahaha:Hello World at "+Time.now.to_s 
  end
end
这时候我们重新打开浏览器会发现已经应用了我们的修改,对于中文输入如果中文显示乱码首先使用UltraEdit查看文件index.html.erb是否是utf8格式的.然后的话可以通过在controller中的application.rb中添加一个filter来进行:
before_filter :configure_charsets       
      
 def configure_charsets       
   response.headers["Content-Type"] = "text/html; charset=utf-8"     
   suppress(ActiveRecord::StatementInvalid) do     
     ActiveRecord::Base.connection.execute 'SET NAMES gb2312'     
   end     
 end 
应该就可以解决了.没有发现这种问题.如果在后面发现,我会专门就这个问题进行研究一下.
好了,上面基本上我们就已经运行成功了一个ROR的Helloworld程序.下面我再对于我现在所使用的开发工具进行一个点单的介绍.我现在使用的就是Aptana的那个RadRails工具来做开发.使用的是当前的最新版本1.2版本;
下载地址:http://ec2-67-202-4-104.z-1.compute-1.amazonaws.com/downloads/current/Windows/Aptana_Studio_Setup.exe
下载之后就可以直接安装,是一个30天试用版本,30天以后就过期了,所以这个了不知道30天后是否能够重装什么的解决问题.还有就是我们可以赏析你一下他的源代码:有关注册这个部分的源代码.
通过Jad工具我们可以查看到com.aptana.ide.core_1.2.1.020234.jar下的com/aptana/ide/core/licensing下的ClientKey这个类.他的代码如下:
// Decompiled by DJ v3.10.10.93 Copyright 2007 Atanas Neshkov  Date: 1/2/2009 3:47:41 PM
// Home Page: http://members.fortunecity.com/neshkov/dj.html  http://www.neshkov.com/dj.html - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   ClientKey.java
 
package com.aptana.ide.core.licensing;
 
import java.util.Calendar;
import java.util.TimeZone;
 
public final class ClientKey
{
 
    public ClientKey(int i, String s, long l)
    {
        type = i;
        email = s;
        expiration = 0x7fffffffffffffffL;
    }
 
    public boolean isCloseToExpiring()
    {
        Calendar calendar = Calendar.getInstance(GMT);
        calendar.add(2, 1);
        return getExpiration().before(calendar);
    }
 
    public boolean isValid()
    {
        return email != null && email != "EMAILS_NON_MATCHING";
    }
 
    public boolean isCloseToMatching()
    {
        return email == "EMAILS_NON_MATCHING";
    }
 
    public boolean isExpired()
    {
        Calendar calendar = Calendar.getInstance(GMT);
        return calendar.after(getExpiration());
    }
 
    public String getEmail()
    {
        return email;
    }
 
    public Calendar getExpiration()
    {
        Calendar calendar = Calendar.getInstance(GMT);
        calendar.setTimeInMillis(expiration);
        return calendar;
    }
 
    public boolean isTrial()
    {
        return type != 1;
    }
 
    public boolean isPro()
    {
        return !isTrial();
    }
 
    public boolean shouldProPluginsRun()
    {
        if(isPro())
            return true;
        else
            return !isExpired();
    }
 
    public static String trimEncryptedLicense(String s)
    {
        String s1 = s;
        s1 = s1.trim();
        s1 = s1.replaceAll("--begin-aptana-license--", "");
        s1 = s1.replaceAll("--end-aptana-license--", "");
        s1 = s1.replaceAll("\\s+", "");
        return s1;
    }
 
    public static final String BEGIN_LICENSE_MARKER = "--begin-aptana-license--";
    public static final String END_LICENSE_MARKER = "--end-aptana-license--";
    public static final int PRO = 0;
    public static final int TRIAL = 1;
    private static final TimeZone GMT = TimeZone.getTimeZone("GMT");
    public static final String EMAILS_NON_MATCHING = "EMAILS_NON_MATCHING";
    public static final ClientKey EMPTY_KEY = new ClientKey(2, "yourname@hotmail.com", 0L);
    private String email;
    private long expiration;
    private int type;
 
}
上面这个类的话拷贝到本地创建一个clientKey类,然后将Email修改为自己的Email进行编译.上面的过期时间使用Long.MAX_VALUE进行设置的..理论上应该也有2-3亿年才能够过期的吧...上面的类只是对于网上的一个破解方式在理论上面的修改.本人并未实际尝试,大家可以测试一下.可以告诉我一下测试结果的.关键公司不让用盗版,用盗版会被开除的...所以只能够使用一个试用版...
安装好了之后就可以通过更新来安装RadRails插件了.更新完毕之后就可以发现可以创建Ruby和Rails项目了,使用了一下感觉这个工具的确是方便了很多,比起手动那样写的话要方便多了..不愧为当前最好的ROR编辑器.使用起来的确很方便,无论是代码的自动提示.就是启动的自动更新和加载Message比较烦人.影响速度.使用起来还是不错的...关闭自动更新和Message,但是Message关闭了竟然还在启动的时候更新Message,还有就是不知道怎么使用功能倒是很强大,但是貌似经常搞到我卡机,所以都不干玩太多进程了.这样的话我们可以使用他的generator来进行创建控制器Model等.或者直接在console下使用命令:
script/generate controller helloworldscript/generate controller helloworld
来进行创建一个helloworld控制器,也可以添加index来创建一个index.html.erb,使用起来既可以使用命令行的方式,也可以使用图形的方式.截图一张
image
好了,这次到此结束了,下一篇我将对ROR数据库连接相关进行讲解...

posted on 2009-01-02 16:15 fonhal 阅读(630) 评论(0)  编辑  收藏 所属分类: ROR 学习笔记系列


只有注册用户登录后才能发表评论。


网站导航: