摘要: 关键字: Rails block view我们可以在application_helper里定义一个admin_area helper方法: ruby代码 moduleApplicationHelper defadmin_area(&block) ifadmin? concatcontent_tag(:div,capture(&black),:class=>'admin'),b... 阅读全文
posted @ 2009-07-08 15:03 麦飞 阅读(211) 评论(0) 推荐(0) 编辑
摘要: action_view/helpers/active_record_helper.rb里的一段代码: ruby代码 moduleActionView classBase @@field_error_proc=Proc.new{|html_tag,instance|"<divclass=\"fieldWithErrors\">#{html_tag}</div>"} cattr... 阅读全文
posted @ 2009-07-08 14:39 麦飞 阅读(325) 评论(0) 推荐(0) 编辑
摘要: 我们可以将Task类里的due_at_string这个虚拟属性的生成做成一个plugin: ruby代码 rubyscript/generatepluginstringify_timeruby script/generate plugin stringify_time 这将在vendor/plugins目录生成如下文件: ruby代码 vendor plugins stringify_time -... 阅读全文
posted @ 2009-07-08 10:56 麦飞 阅读(147) 评论(0) 推荐(0) 编辑
摘要: 关键字: rails in_groups_of 这次讲的是active_support/core_ext/array/grouping.rb里的in_groups_of方法 ruby代码 #%w(1234567).in_groups_of(3){|g|pg} #["1","2","3"] #["4","5","6"] #["7",nil,nil] # #%w(123).in_groups_of(2... 阅读全文
posted @ 2009-07-08 10:29 麦飞 阅读(1011) 评论(0) 推荐(0) 编辑
摘要: 这东西现在已经是苹果机上内置的命令行工具之一了,可见其魅力之一斑 1)二话不说,先从这里开始吧! curl http://www.yahoo.com回车之后,www.yahoo.com 的html就稀里哗啦地显示在屏幕上了~~~~~ 2)嗯,要想把读过来页面存下来,是不是要这样呢? curl http://www.yahoo.com > page.html当然可以,但不用这么麻烦的!用cur... 阅读全文
posted @ 2009-07-07 17:51 麦飞 阅读(324) 评论(0) 推荐(0) 编辑
摘要: 这次就是讲用_count字段来缓存has_many的计数 看Project和Task的例子: ruby代码 <h1>Projects</h1><table><%forprojectin@projects%><tr><td><%=link_toproject.name,poject_path(project)%>&l... 阅读全文
posted @ 2009-07-07 15:56 麦飞 阅读(1104) 评论(0) 推荐(0) 编辑
摘要: 看这个页面代码: ruby代码 <%fortaskin@tasks%><%=link_totask.name,task_path(task)%>in<%=task.project.name%><%end%><% for task in @tasks %> <%= link_to task.name, task_path(task) ... 阅读全文
posted @ 2009-07-07 15:28 麦飞 阅读(234) 评论(0) 推荐(0) 编辑
摘要: 我们在application.rhtml(global layout)里经常需要写各种flash的显示: ruby代码 <%unlessflash[:notice].nil?%><divid="notice"><%=flash[:notice]%></div><%end%><%unlessflash[:error].nil?%>... 阅读全文
posted @ 2009-07-07 14:48 麦飞 阅读(249) 评论(0) 推荐(0) 编辑
摘要: 一般来说layout有如下五种: gobal layout,controller layout,shared layout,dynamic layout,action layout dynamic layout 有时候我们需要根据不同的用户角色来使用不同的layout,比如管理员和一般用户,比如博客换肤(也可以用更高级的theme-generator) ruby代码 classProjectsCo... 阅读全文
posted @ 2009-07-07 13:46 麦飞 阅读(1285) 评论(0) 推荐(0) 编辑
摘要: class Symbol # Turns the symbol into a simple proc, which is especially useful for enumerations. Examples: # # # The same as people.collect { |p| p.name } # people.collect(&:name) # # # The same a... 阅读全文
posted @ 2009-07-07 13:37 麦飞 阅读(232) 评论(0) 推荐(0) 编辑
摘要: with_scope后面可以跟一个hash{方法名=>参数hash},方法名可以是:find和:create,:find的参数hash包含:conditions,:joins, :include, :offset,:limit, and :readonly options.:create后的参数hash是属性列表。 class Article { :conditions = "blog... 阅读全文
posted @ 2009-07-07 11:18 麦飞 阅读(837) 评论(0) 推荐(0) 编辑
摘要: classTasksController<ApplicationController defincomplete @tasks=Task.find(:all,:conditions=>['complete=?',false]) end endclass TasksController < ApplicationController def incomplete @tasks = ... 阅读全文
posted @ 2009-07-07 10:36 麦飞 阅读(2974) 评论(0) 推荐(0) 编辑
摘要: 安装will_paginatesvn:svn://errtheblog.com/svn/plugins/will_paginategit:script/plugin install git://github.com/mislav/will_paginate.git在Model中使用will_paginate #models/product.rb defself.search(search,page... 阅读全文
posted @ 2009-07-06 17:19 麦飞 阅读(320) 评论(0) 推荐(0) 编辑
摘要: Gmail里的Ajax自动保存草稿在Rails里的实现 首先在layout里引入Javascript标签 [coce] <%= javascript_include_tag :defaults %> posts_controller.rb Java代码 defnewifrequest.get?@post=session[:post_draft]||Post.newelse@post=P... 阅读全文
posted @ 2009-07-02 11:52 麦飞 阅读(456) 评论(0) 推荐(0) 编辑
摘要: 有时候我们可能要允许用户使用某些HTML标签,但是必须禁止另外一些HTML标签 我们可以在数据库存储用户输入的内容,包括允许的HTML标签,然后显示时过滤一下 让我们来一个helper方法来过滤内容,有两种方式: 1,写在application_helper.rb里 2,写在lib目录里,然后在config/environment.rb里加上require_dependency 'rails_pa... 阅读全文
posted @ 2009-07-02 11:26 麦飞 阅读(282) 评论(0) 推荐(0) 编辑
摘要: The following is a list of all of the methods public methods offered by the JavaScriptGenerator. These methods are called on the page object in your RJS templates.Since RJS is all about generating Jav... 阅读全文
posted @ 2009-07-02 10:57 麦飞 阅读(230) 评论(0) 推荐(0) 编辑
摘要: 由于database.yml十分小巧,我们很容易忘掉它是用YAML这种非常健壮的标记语言来写的。 YAML有一个叫"merge keys"的特性,它可以merge one Hash into another。 例如: Java代码 development:adapter:mysqldatabase:DRYUpYourDatabaseConfig_developmentusername:rootp... 阅读全文
posted @ 2009-07-01 21:34 麦飞 阅读(205) 评论(0) 推荐(0) 编辑
摘要: Rails的Template可以访问的对象 1,Controller里所有的实例变量 2,Controller Environment里的request, params, cookies, response, session, headers对象 3,表示当前的Controller的controller对象,这样则Template可以调用Controller里的public方法 4,表示模板根目录... 阅读全文
posted @ 2009-07-01 20:32 麦飞 阅读(133) 评论(0) 推荐(0) 编辑
摘要: Active Record有一些富有“魔力”的列名: created_at, created_on, updated_at, updated_on 在create或者update一行时Rails对_at形式的列用timestamp自动更新,对_on形式的列用date自动更新 lock_version 如果一个表有lock_version这个列,则Rails会跟踪一行的版... 阅读全文
posted @ 2009-07-01 20:30 麦飞 阅读(207) 评论(0) 推荐(0) 编辑
摘要: MRP启动之后,上次介绍了Phase I的完成情况,之后很久都没什么时间去折腾,最多是零零散散的更新几行代码——事实上对于大部分页面来说,也就几行代码就行了——直到开始实现查询部分的功能,这个有点麻烦,这次趁着假期才算是完成了这个部分,于是就有了这个最初的发布版本。这一篇就针对这个过程中的一些体会稍作回顾,主要涵盖的是Controller和View的... 阅读全文
posted @ 2009-06-26 16:26 麦飞 阅读(1108) 评论(0) 推荐(0) 编辑