Join us for GZRUBY9: Wed Nov 21, 7:30PM

The 9th gzruby meetup will be coming to the kudelabs offices in Guangzhou next Wednesday, November 21st at 7PM. We'd love to see you there! You'll meet several members from the growing Ruby community in our city, and have a chance to present an idea you're working on or a gem you use. Feel free to invite anyone who may be interested in ruby, or learning more about Ruby on Rails.

Some small snacks and drinks provided. Many thanks to continued support from Strand Beer and this month's sponsor: Beansmile

See more about this event at gzruby.org: gzruby9 - Nov 21, 7:30PM

gzruby is the Guangzhou Ruby User's group, we meet once every 2 months to share tips and techniques, as well as show off recent projects.

Posted by adevadeh 15 Nov 2012 at 02:58AM


Rails on Campus: 华农版 - Session #3

The excellent students at South China Agriculture University

Rails on Campus Roundup / Rails on Campus 大聚集


Last Saturday we concluded the Rails on Campus: 华农版 (SCAU) program with session #3. We wish to thank Dean Song, Professor Sun, and all the students who took the time to attend. We really appreciate working with such amazing students, and we were very happy to be able to come to your school.

上周六我们在华农顺利地进行了Rails on Campus 第三期活动。在此非常感谢宋主任,孙教授以及所有到场的学生。 真的很难得可以跟这么多这么优秀的学生一起分享和学习, 我们也很高兴能够来到你们学校。

And special congratulations to 欧振聪 Clarence Au (@onionou) the winner of our coding competition!

同时,我们在这里恭喜欧振聪同学@onionou赢了我们组织的这场代码比赛!

Over the course of 3 sessions, we've introduced the students to the basics of Rails, and hopefully they will be able to keep learning about Rails and ruby. To recap, here's what we've gone over:

通过这三次活动,我们向学生们分享了一些Rails的基本知识,希望他们可以在课余时间继续学习Rails和Ruby。概括一下,我们谈及的有以下方面的话题:

  • Getting the Rails environment - RailsInstaller.org, ruby-lang.org, SublimeText
  • Basic ruby syntax - messages.each {|m| puts m}, hash = {:key1=>'val1'}
  • Creating a new Rails app from scratch - rails new miniblog
  • Using scaffolding to get started quickly - rails generate scaffold message body:text
  • Creating associations between models - message: has_many :replies, reply: belongs_to :message
  • Using Rails view helpers to build forms - form_for [:message, Reply.new]
  • How to access the Rails session - session[:user_id]
  • Writing filters to manage authentication - before_filter :require_login
  • Custom routes for easy URLs - match 'login' => 'sessions#new'
  • Deploy your app to share with the world - heroku, DotCloud

The result of our work in class should be a working miniblog that you can share with your friends. Anyone can login, post messages, and reply to other users' messages.

在课堂上我们的练习任务是:创建一个miniblog, 通过这个miniblog 你可以跟朋友分享,每个人都可以登录, 发表信息同时也可以回复其他用户的信息。

We'd love to see you continue with your code. Add more features, improve the design, and put it up on heroku! Make sure to let us know via the Rails on Campus Weibo account.

我们非常希望你们可以继续写代码,让你的miniblog变得更好。 增加更多的功能,做更好的设计,然后放上heroku! 在微博上@RailsOnCampus 展示你的成果吧!

We also would appreciate any feedback, so please let us know how we can make the event go more smoothly next time, or if there's anything else we should cover. We hope to take this program to other universities in the area, and perhaps come back to SCAU for a second season next year.

欢迎对我们的活动提出您的宝贵意见! 这对我们很重要,希望我们下一次可以组织得更好,活动开展得更顺利。如果你有感兴趣的话题希望我们可以分享,欢迎给我们来信! 我们希望到广州更多的大学去分享,可以的话也希望明年回到华农分享更多有趣的话题!

Session #3 / 第三届


The third session in our program focused on completing the feature set to make our miniblog a full web application. Of course we needed a way for users to log in, so that we know who says what. To that end, we went over how to track user sessions, and how to use before_filters to manage users and make sure they log in when they need to.

在第三期我们会分享一些关于如何给我们的miniblog添加新功能新应用,使其变成一个更加完整的网络应用。 首先,我们必须想个办法让用户可以登录,这样我们才知道谁说了什么。然后我们也分享了如何去追踪用户及其发表的信息,以及如何使用before_filters 去管理用户,让他们在必要时进行登录。

Links:


Rails on Campus is a program started by Leon Du and Shaokun Wu, 2 ruby developers in Guangzhou, China. The idea is to build a good set of material for introduction courses that can be used to teach Rails on college campuses. College students are often not exposed to the latest development techniques, and are left to learn on the job. The goal of this program is to introduce students to the world of open source development frameworks to help prepare them for a good job.

Rails on Campus是由中国广州两位Ruby 开发者, Leon Du和伍少坤发起的。活动的初衷是为大学Rails的教学积淀一些有价值的学习材料。大学学生接触新技术的机会不十分多,而常常要上到工作岗位才开始学习。希望通过这次活动,可以为同学们带来更多开源的开发框架,以此为将来的好工作更好地准备自己。

Rails on Campus is sponsored by:

Rails on Campus的赞助团队有:

Posted by adevadeh 08 Dec 2011 at 04:08AM


Introducing gems.gzruby.org

gems.gzruby.org

As those of us living and working in China know,sometimes the internet can be troublesome . At some point last month, installing gems from the command line - something we do almost every day if not many times a day - no longer worked seamlessly. For whatever unknown reason, the rubygems production server is currently inaccessible or extremely unstable from within mainland China.

In discussions with the gzruby group, we tried to find workable solutions to this problem. One idea is to set up a reverse proxy. This solves the issue, but installing gems is still really slow. Another proposal is to set up a mirror, as described in this post by sachin. But rather than set up a local server, why not set up the proxy somewhere the greater community can use it.

Thus, gems.gzruby.org was born.

Setup

Add the following line to your Gemfile:

source "http://gems.gzruby.org/"

And change your ~/.gemrc to something like this:

---
:sources:
- http://gems.gzruby.org/
gem: --no-rdoc --no-ri

Code

The project was started by fnando. Our fork contains cosmetic changes only. If you want to set up your own proxy/cache, we recommend that you start from this fork by akitaonrails.

The app itself is a simple rack app that either serves a cached version of the gem, or fetches the gem from production.cf.rubygems.org. So the server on which you install must have access to this address.

If you already have a rails server running passenger on any VPS outside of china, setup is very easy. Simply clone the project into a new directory, and add a config to apache or nginx just as you would for any rails app.

Posted by adevadeh 28 Nov 2011 at 05:22AM


Join Us for the third GZRUBY meetup

大家好!

时间过得飞快,Rubyist party 又来了,你准备好了吗? 有主题想分享的同学请踊跃回复啦!

时间:23/11,  7:30 PM
地图Kudelabs 明月一路59号, 汉苑 西塔601室
地铁:五羊邨A出口

Dear Rubyists,

How time flies, it's party time again, are you ready?
Anyone want to share something, please reply!

When: 23/11,  7:30 PM
Where: Kudelabs, 明月一路59号, 汉苑 西塔601室
Metro: Exit A Wuyangcun station

So far, we have  a few talks planned: @shaokun will talk about Rails Engines. @seb will discuss large scale rails apps and scaling issues, and @adevadeh will talk about solving the rubygems problem. 

Posted by adevadeh 17 Nov 2011 at 02:53AM


Join us for the second gzruby meet up!

The second meeting of the gzruby group will be held in the Kudelabs Offices Wed, Sept 21 at 7:30PM.

Anyone in GZ who uses ruby, or is interested in ruby is invited!

For more information, join the mailing list: http://groups.google.com/group/gzruby

大家好!

本周三我们将会在老地方(Kudelabs)迎来又一个Rubyist party,大家准备好了吗? 为了照顾大家出行,避开下班高峰期,我们决定这次聚会稍晚(晚7:30)开始, 以便大家就餐,来到后可以直接进入主题进行讨论。 聚会预计持续1.5小时,出去开场介绍和自由讨论之外,应该有一个小时左右留给大家做发言, 目前我们已经有来自Sooyoung团队的Padrino主题,此外我们团队也将带来一个主题, 分享我们项目开发过程中使用的一些gem/工具,现在还留有时间可以准备1-2个主题, 大家有想来做分享的请踊跃回帖阿!

Hi all dear rubyist in GZ, it's party time again!

We'll have the next rubyist party on Wednesday(21th Sept) this week at 7:30 pm, same place at Kudelabs. To make it more convenient to all of us, we decide to get start a little later, so everyone can have dinner before come to the party, then we can get straight into the topics faster. We planned to have the event last in around 1.5 hrs, endding at around 9:00 pm, before everyone is sleepy ;-) besides the introduction/public discuss section, we should have about 1 hours for topics. We've already had Padrino from Sooyoung's team, our team will share a topic, something like "share our toolbox", so we still have time for 1-2 topics, if you have something to share, please post it here :)

Posted by adevadeh 19 Sep 2011 at 08:08AM


Simple SVNHelper for Rails Applications

原创:黄文斌
来自:http://github.com/kudelabs/SVNHelper/tree/master 这个小程序是专门写给使用SVN版本控制的Rails应用程序。
有了它,我们可以在某个特定的网页上面直接看到我们的程序revision版本,还有最后的修改日期。
它的实现原理很简单,就是查看.svn/entries文件里面的修改记录。
一般用法,把这个文件复制到root/lib目录下面,然后在environment.rb里面用全局变量记录它:
$current_version = SVNHelper.version(RAILS_ROOT)
这样子,你可以在erb页面里面调用这个变量,比如:
程序版本: $current_version.rev_with_date
同时,它会在每次服务器重启的时候重新记录最新修改的svn版本。
This little library is specialized for those Rails Applications which are using SVN as version control. It allows us to view the revision and the last modified date of our application on the website. It’s rather simple to implement such function, we just look into‘.svn/enries’file in RAILS_ROOT and record the things we want inside.
How to use it?
Please copy the file into root/lib, and set up a global variable in config/environment.rb:
 $current_version = SVNHelper.version(RAILS_ROOT) 
And then you can use this variable everywhere you wish to see the revision and last modified date. Also, its value may be changed each time you restart the server if any changes to the SVN found. If you are interested in this, you can see the source code from here: http://github.com/kudelabs/SVNHelper/tree/master

Posted by Mysen 12 Jul 2009 at 04:05AM


来自asciicasts.com文本形式的Rails动态短片

这对于我们早期贴出的Rails动态短片是一个很好的更新。


来自英国的Rails程序员Eifion Bedford正在收集Ryan Bates系列的文本形式的免费动态短片。这意味着他是精心抄写Ryan的著作,整理截图,您可以学习这些代码示例并复制和粘贴它们。这是一个很好的学习这些材料的途径,尤其对于非英语母语的人来说。他所称的ASCII Casts,是一个巨大的免费学习Ruby and Rails的额外材料。我推荐安全系列性能提示。感谢Eifon努力为我们描绘生动的网页开发世界。

Posted by 钟宇平 25 Feb 2009 at 04:11PM


Rails 2.x Named Scope

我在实际项目中发现 named_scope 的好处,下面翻译了 What’s New in Edge Rails: Has Finder Functionality 这篇文章,同时结合自己使用的心得与 Rails 爱好者们共享。

原文

Rails 2.x 整合了 Nick Kallen 的 has_finder plugin,改名为 named_scope。首先,在 User Model 中定义 named_scope:

<pre> class User < ActiveRecord::Base named_scope :active, :conditions => {:active => true} named_scope :inactive, :conditions => {:active => false} named_scope :recent, lambda { { :conditions => ['created_at > ?', 1.week.ago] } } end </pre>

标准的使用:

<pre> User.active # 相当于 User.find(:all, :conditions => {:active => true}) User.inactive # 相当于 User.find(:all, :conditions => {:active => false}) User.recent # 相当于 User.find(:all, :conditions => ['created_at > ?', 1.week.ago]) </pre>

不同的 named_scope 之间可以嵌套使用:
<pre> User.active.recent </pre>

这相当于下面的用法:
<pre>

  1. User.with_scope(:conditions => {:active => true}) do
  2. User.find(:all, :conditions => [‘created_at > ?’, 1.week.ago])
  3. end

高级使用:

named_scope 允许在执行时,传递参数来定义查询条件。
<pre> class User < ActiveRecord::Base named_scope :registered, lambda { |time_ago| { :conditions => ['created_at > ?', time_ago] } end </pre>

User.registered 7.days.ago # 相当于 User.find(:all, :conditions => [‘created_at > ?’, 7.days.ago])

named_scope 扩展

与 association extensions 相似, named_scope 也可以扩展。

<pre> class User < ActiveRecord::Base named_scope :inactive, :conditions => {:active => false} do def activate each { |i| i.update_attribute(:active, true) } end end end </pre>

<pre>

  1. 重新激活所有未激活用户
    User.inactive.activate

匿名 Scopes

通过对类对象 class objects 使用 scoped 方法,你可以快速地建立 scope。

<pre>

  1. 定义 named scopes
    active = User.scoped(:conditions => {:active => true})
    recent = User.scoped(:conditions => [‘created_at > ?’, 7.days.ago])
  1. 嵌套使用
    recent_active = recent.active
  1. 对返回的对象进行操作
    recent_active.each { |u| … }

=================

单纯的 scope ,例如 User.active, User.inactive 等只是提供简单的快捷方法,并不具备强大的功能特性。我们在实际项目中,很多时候是得益于 scope 的嵌套使用。我认为嵌套使用主要有五个好处:

1. scope 的嵌套使用,能够多层次(只要符合逻辑,几乎为无限)的组合查询条件。例如User.active.recent, User.active.recent.female, User.active.recent.female.adult……

2. 能够与 association 结合使用,限定外部 scope。举个例子,假如 User Belongs To Club,你可以将查询限制到具体一个 Club 的范围,如 Club.first.users.active.recent。

3. 用面向对象的方式,使得复杂查询在一个 SQL 语句中完成,减少编写复杂 SQL 语句的次数。例如 User.active.recent.female.adult,该查询只会触发一次 SQL 语句,而且非常简洁。在过去我们只能通过类似下面的语句来完成该查询:
User.find(:all, :conditions => [“active = true AND created_at > ? AND gender = ‘female’ AND age > 25”, 1.week.ago]。

4. 符合 DRY 精神,精心定义的 scope,可以避免很多重复的 SQL conditions,其复用性相当高!

5. scope 返回的对象仍然具备所有 SQL 查询功能。例如,User.active.rencet.find_by_first_name(“Jim”),或User.active.recent.find_all_by_last_name(“Green”),同样可以工作,而且仍然只触发一次 SQL 查询。

Named scope 需要注意的问题:

1. 嵌套 scope 只能提供交集查询,如果要查询多个 scope 的并集,只能通过数组的 + 操作。例如,User.active + User.recent。但是这样做的一个问题是返回的结果为普通的数组,我们无法再使用上述第5个好处了。针对该问题,我们的处理方法是在必要时候,定义更灵活的 scope。例如,使用 scope 的参数传递功能:

<pre>

named_scope :active_or_recent, lambda { |args*|
time_ago = args.first

if time_ago { :conditions => [‘active = true OR created_at > ?’, time_ago] } else { :conditions => [‘active = true’] } end

}

这样,User.active_or_recent(7.days.ago) 使用的条件为{ :conditions => [‘active = true OR created_at > ?’, time_ago] },而 User.active_or_recent 使用的条件为{ :conditions => [‘active = true’] }。

2. 突然忘了,请等待更新:)

Posted by Shaokun 18 Sep 2008 at 08:46PM


railscasts(Rails短片)帮助你成为ruby高手

click here to read the English vesion of this article
点击这里浏览本篇文章的英文版

随着Rails越来越受欢迎,用于学习Rails的资源也越来越多。其中最好的一种学习方式就是看那些关于Ruby和Rails的视频短片(ScreenCasts)。这些短片让你在听讲授者解释概念的同时可以看到相应的代码。通常,这些短片对应一些经典的教材,但是通过这种视频短片的方式,这些内容变得更容易理解,你可以更加直观的感受到那些枯燥的概念背后的真正的含义。

首先,有一些优秀而且免费的由Ryan Bates讲授的关于Rails的短片可以从这里下载:railscasts.com。他们短小精悍,常常重点突出某一个新的特征。你可以用iTunes把这些短片自动下载到你的电脑里(但是这在中国大陆行不通,因为Feedburner被拦截了),或者你可以直接从网站下载。这些短片里面有一组是专门讲述Rails 2.1 的新特征的,很值得一看。

其次, Screencast之父, @topfunky a.k.a Geoffrey Grosenbach 也已经整理好了一组出色的关于Rails的培训教材,并把它们放在了peepcode.com上。这些教材是收费的,但是并不算贵,每个视频只要9美元即可(考虑到美国的消费水平,的确是相当便宜了)。这些教材很优秀,含金量很高,物超所值。我们已经学习过这些短片,它们帮助我们跟上了新版本rails发展的速度,此外我们也从那里下载了一些关于Rails的PDF书籍。我们极力推荐从这些教材入手学习Rails。

Rails运行在Ruby环境,我也找到了一些很好的关于如何使用Ruby编程的视频短片(ScreenCasts)。Dave Thomas of The Pragmatic Programmers(也就是Web开发敏捷之道(Agile Web Development with Rails)的作者)亲自在短片中讲授了Ruby对象模型,类继承和元编程(meta-programming)的基本概念。它们能够帮助你充分发挥Ruby的强大威力,并帮你建立理解Rails框架的基础知识背景。这些视频短片(ScreenCast)只要5美元一个(别误会,我可不是他们的推销员,如果他们付给我报酬的话,我倒是很乐意帮他们推销一下)。

如果你真的对Rails和Ruby感兴趣,我强烈推荐你使用以上这些网络上的学习资源,当然你要需要下载相关的工具,并准备好开始写你自己的Rails代码。

尽情享受Rails把,未来的Ruby黑客们!

Posted by 张晓峰 16 Jun 2008 at 08:10PM


Javascript 通用筛选功能

如何对 HTML 上的元素,如表格,列表或者 DIV 等等按照关键字进行筛选呢?最简单的方法是通过循环遍历各个元素,对比关键字与元素的 inner html 内容。但是,这个方法有一个很大的缺陷。例如,如果对跨行的表格 tr 进行筛选呢?在显示上,跨行的 tr 与其他 tr 显示在同一行,但是它们的 inner html 往往却不同。而代码上,它们又在不同的 tr 上。所以,隐藏了跨行的 tr 会破坏整个表格的布局。对于这个问题,我们的解决方案是对任何可筛选的元素添加匹配字符串变量,这样,筛选功能就变得更加通用和灵活了。因为,就算 tr 的内容不一样,我们仍然可以在代码中设置相同的匹配字符串变量,使其在筛选中被同样处理,显示或者隐藏。下面我们简单介绍筛选功能的 javascript:

示范例子

<pre>

… … … … …
菜系 菜名 价钱
粤菜烧汁茄子10.00
烧鸭20.00
西芹炒百合15.00
东北菜酱骨架35.00
火龙鱼70.00
湘菜xxxx.xx
潮菜xxxx.xx
越菜xxxx.xx
寿司xxxx.xx
清真xxxx.xx


<pre> function strip_spaces(stripee) { return stripee.replace(/(^[ ]*) | ([ ]*$)/g,''); }

function filter(id, criteria) {
var t = $(id);
if (!t) { return; }

t.getElementsByClassName(‘no_matched_notice’).each(function(elem) { elem.remove(); }); var notice_msg = ‘No matching data found!’; criteria = strip_spaces(criteria);

empty_input = criteria.length === 0;

var matched_none = true; t.childElements().each(function(elem) { // if the user doesn’t input anything, then show all the elements if (!elem.matched_strs || empty_input) { Element.show(elem); } else { // else find the element matched = false; elem.matched_strs.each(function(str) { if (str.toUpperCase().indexOf(criteria.toUpperCase()) != -1) { matched = true; matched_none = false; return; } }); matched ? Element.show(elem) : Element.hide(elem); } }); if (matched_none && !empty_input) { var tag = t.tagName; var ins = null; switch (tag) { case “TBODY”: case “TABLE”: ins = new Insertion.Bottom(t, “”no_matched_notice\“>” + notice_msg + “”); break; case “OL”: case “UL”: ins = new Insertion.Bottom(t, “”no_matched_notice\“>” + notice_msg + “”); break; default: ins = new Insertion.Bottom(t, “<” + tag + " class=\“no_matched_notice\”>" + notice_msg + “</” + tag + “>”); break; } }

}

在 HTML中,我们需要做的工作是:

1) 添加匹配字符串到所有可匹配元素上,为了能够匹配多个关键字,将以数组的形式存储:
<pre> $('tr_01').matched_strs = ["粤菜", "烧汁茄子", "烧鸭", "西芹炒百合"]; $('tr_02').matched_strs = ["粤菜", "烧汁茄子", "烧鸭", "西芹炒百合"]; $('tr_03').matched_strs = ["粤菜", "烧汁茄子", "烧鸭", "西芹炒百合"];

$(‘tr_04’).matched_strs = [“东北菜”, “酱骨架”, “火龙鱼”];
$(‘tr_05’).matched_strs = [“东北菜”, “酱骨架”, “火龙鱼”];

2) 添加筛选关键字的输入框:
<pre> <input id="keyword" type="text" /> </pre>

3) 绑定 filter 函数到输入框事件上:
<pre>

$(‘keyword’).onkeyup = function() {filter(‘food’, this.value);};
$(‘keyword’).onchange = function() {filter(‘food’, this.value);};

恭喜你,完成了!

Posted by Shaokun 04 Dec 2007 at 04:33PM



>