Posts Tagged ‘rails’

专业与否仅一步之差

June 25, 2008 Tags:

这是一本讲述 rails 和 google maps 开发的书。

Google Maps Book

很遗憾,我买了一本中文版的。暂且不说翻译质量如何,让我们来看看封面吧:

Google Maps Book

专业不专业,往往仅一步之差。

BTW,这个译本是机械出的。

Meta on Rails plugin for meta tags

June 25, 2008 Tags: ,

Meta on Rails is a very simple rails plugin to help you customize your pages’ meta tags a little bit easier.

How to Install

Meta on Rails is hosted on github. For Rails 2.1 or later, install in this way:

./script/plugin install git://github.com/ashchan/meta_on_rails.git

For Rails 2.0.2 or older, need to clone the public git url:

git clone git://github.com/ashchan/meta_on_rails.git vendor/plugins/meta_on_rails

Usage

Add the following code to the layout (e.g. app/views/layout/application.html.erb), and be sure to put it in the head tag:

<head>
<%= display_meta(:keywords => "default,keywords", :description => "default description") %>
</head>

Values passed to the display_meta method becomes the site-wide default values. In the above example, these two meta tags will be generated on all pages if they’re not overridden on the views:

<meta name="description" content="default description" />
<meta name="keywords" content="default,keywords" />

Then add this code to the view to set meta tags on that page:

<% set_meta(:keywords => 'my,keyword', :generator => 'a bad <script /> generator') %>

The output html will be like this (note that html tags are tripped):

<meta name="generator" content="a bad  generator" />
<meta name="description" content="default description" />
<meta name="keywords" content="my,keyword" />

Dead simple, right?

Feedback

Feel free to leave a message if you have any suggestion or find bugs.

Freelancing 并不等于 Free

June 24, 2008 Tags: , ,

告别全职工作已有数月了,四月份正式开始 freelancing 至今也已经有两个月 。切身感受到 Freelancing 并不意味着完全 free,因为:

  • 不够牛,所以没有高回报的项目;
  • 什么活都有进度压力,有时(确切得说是大部分时间)会比全职上班更辛苦;
  • 活多了会忙不过来,项目青黄不接时会没有饭吃;

放弃上班却正是因为 freelance 的 free 这一点。不用看老板脸色,不用朝九晚五,可以去看周二半价电影,可以自己选择要做的东西,可以在工作时间看书看碟。当然,享受这一切的前提是,要有够用的奶粉钱可以赚。

计划对 ashchan.com 再进行一次修改,使用 haml 作为 view template engine,并把外观简单化。

本人主要接 Rails 和 Ruby 相关的活。如果你有项目需要 outsourcing ,或者你有项目要寻求技术合作并且希望合作地在杭州,可以与我联系

Leopard上试玩mod_rails

April 26, 2008 Tags: , , , ,

Setup mod_rails Passenger Mac OS X Leopardmod_rails官方网站上的Passenger users guide中推荐的在OS X上安装mod_rails的教程文章。文章描述的很详细,安装过程主要花在编译安装apache上(Leopard自带的apache支持passenger-install-apache2-module有问题)。我照着步骤操作后,以development模式放了一个测试站点,运行后站点跑了起来,但样式和图片无法显示。显然是权限有问题。

解决方法:在站点的配置中加入对所在目录的访问权限。比方说虚拟站点的apache设置为:

<VirtualHost *>
  ServerName test.ashchan.com
  DocumentRoot /Users/james/codex/ashchan.com/public
  RailsEnv development
</VirtualHost>

则加入以下配置即可:

<Directory "/Users/james/codex/ashchan.com/public">
   Options FollowSymLinks
   AllowOverride None
   Order allow,deny
   Allow from all
</Directory>

站点起来时(第一次访问)感觉速度比较慢,不知道production模式下会快多少。相比mongrel cluster等每次需要重启的麻烦,mod_rails直接上传部署并自动扩展的方式非常吸引人,相信不用多久便会非常流行。

Rails中使用Stone

April 21, 2008 Tags: , , ,

Stone是一个数据持久化Ruby库。它把数据保持至文件中(ymal格式),特点是快速易用,因而特别适用于小规模的应用。

要在Rails中使用Stone是非常简单的。我们来创建一个简单的Todo应用:

首先安装Stone:

sudo gem install stone

创建Rails应用:

rails todo

在 config/initializers 中新建一个文件,命名为 stone.rb,文件内容如下:

require 'stone'
Stone.start(Dir.pwd, Dir.glob(File.join(Dir.pwd,"app/models/*")), :rails)

接下去,去掉 ActiveRecord 的使用。在 config/environment.rb 中加入:

config.frameworks -= [ :active_record ]

准备工作完成了。现在使用Stone的stone-gen命令来生成一个model:

stone-gen model Todo title:string

生成后,可以发现 app/models 目录下自动创建了一个 todo.rb 文件,其内容如下:

class Todo
    include Stone::Resource

    field :title, String
end

好了,model有了,我们偷偷懒,用scaffold来创建一个controller(因为我们已经有了model,所以加上–skip-migration参数):

script/generate scaffold Todo --skip-migration

自动生成的controller和view还跑不起来,因为Stone的model方法跟ActiveRecord有所区别,需要调整一下。我们先来改各个view:

在new.html.erb中的表单中,加入:

<p><label>Title:</label><%= f.text_field :title %></p>

在show.html.erb最上边,加入:

<%= h @todo.title %>

其他view先不管了。接下去修改todos_controller:

将index中的:

@todos = Todo.find(all)

修改为:

@todos = Todo.all()

将show中的:

@todo = Todo.find(params[:id])

修改为:

@todo = Todo[params[:id]]

好了,用script/server把应用跑起来,测试一下。访问http://localhost:3000/todos/new,创建一个新的todo:

Stone new

创建成功后,访问http://localhost:3000/todos/1,显示也正确:

Stone Show

不错,果然是非常易用。那么Stone把数据存到哪去了呢?答案是在app/datastore下。在这个目录下可以找到一个todo子目录,里边有刚创建的todo对象的数据文件1.yml,文件内容为:

--- !ruby/object:Todo 
errors: !ruby/object:Validatable::Errors 
  errors: {}

id: 1
title: "tada #1"

结论:Stone很好很强大,待这个库发展更成熟一些后,可以在规模较小又不计划使用MySql或其他数据库的应用上使用。

保护Rails应用的svn目录

March 29, 2008 Tags: , , ,

Rails开发者喜欢用Capistrano来自动化部署网站应用(什么,你不用?),即使不使用自动化部署,很多人也会使用svn co直接将代码取到网站应用目录。这给网站信息泄漏提供了方便。使用下面的地址,就能访问到这样的网站的svn信息:

  • http://somesite.com/.svn/entries
  • http://somesite.com/.svn/javascripts/entries

这可不大妙,因为这个文件里包含了你的svn库的地址、用户名和当前目录内容等很多信息。最好通过服务器配置来禁止对.svn目录的访问。在nginx下,可以在server配置下加入这一条:

location ~ /\.svn {
    deny    all;
}

另一种方式是在config/deploy.rb中加入以下设置,使用svn export来导出代码:

set :deploy_via, :export

不过很多时候为了手工维护站点的需要,我更倾向于用svn co(svn checkout)。

Lovd By Less: 基于Rails的开源社会性网络平台

March 20, 2008 Tags: ,

Lovd By Less是一个以Ruby on Rails构建的开源社会性网络平台。它包含以下特性:

  • 朋友间联系(关注、通信…)
  • Blog
  • 相册
  • Flickr集成
  • YouTube集成

对我来说,Lovd By Less有两点最吸引人,一是它用Rails(2.0.2)开发,二是它开源。这意味着这个平台很有参考价值。

下载并阅读Ruby code中。

attachment_fu使用自定义域名的S3服务

March 2, 2008 Tags:

Rails插件attachment_fu提供非常棒的文件上传管理功能。它支持文件系统、数据库及Amazon S3三种方式来存储上传的文件。

要使用S3这种存储方式,仅需在作为上传对象的Model类中(以Asset为例)调用带:storage => :s3选项的has_attachment方法。实际效果是,上传文件后,通过访问新创建的Asset对象的public_filename方法,便可获得形如http://s3.amazonaws.com/mybucketname/assets/1/file.ext这样的URL。

Continue reading»