每天一剂Rails良药知识库


未分类文章

Rails宝典之七十式: 自定义Routes

我们希望按如下url来查看articles archive: http://localhost/articles http://localhost/articles/2007 http://localhost/articles/2007/9 http://localhost/articles/2007/9/28 我们可以这样定义routes: # in routes.rb map.connect ...
hideto 评论 (0) 有 1118 人浏览 2011-05-26

Rails宝典之六十九式: Markaby in Helper

看一个打印error_message相关的html的helper: module ApplicationHelper def simple_error_message_for(object_name) object = instance_variable_get("@#{object_name}") return if object.errors.empty? result = ...
hideto 评论 (0) 有 758 人浏览 2011-05-26

Rails宝典之六十八式:OpenID Authentication

使用ruby-openid这个gem和open_id_authentication插件做OpenID认证 sudo gem install ruby-openid ruby script\plugin install open_id_authentication rake open_id_authentication:db:create # routes.rb map.open_id_com ...
hideto 评论 (0) 有 847 人浏览 2011-05-26

Rails宝典之六十七式:restful_authentication

restful_authentication是Rails的一个认证插件,基于REST方式,适合RESTful的geek 安装: ruby script/plugin install http://svn.techno-weenie.net/projects/plugins/restful_authentication/ ruby script/generate authenticated use ...
hideto 评论 (0) 有 991 人浏览 2011-05-26

Rails宝典之第六十六式:自定义Rake任务

Rake的强大之处你要自己使用才有切身体会。 我们经常用rake db:migrate来做数据库迁移 我们还可以使用rake -T来列出可得到的rake任务 让我们在store这个Rails程序中创建一个自己的Rake任务 我们创建store/lib/tasks/ryanb.rake: task :greet do puts "Hello World!" end task ...
hideto 评论 (0) 有 1603 人浏览 2011-05-26

Rails宝典之第六十五式: Stopping spam

这次要介绍的是使用Akismet来预防垃圾comment akismetor是作者写的一个插件:http://svn.railscasts.com/public/plugins/akismetor 使用akismetor需要如下几步 1,给comments表添加几个字段: add_column :comments, :user_ip, :string add_column :comments, ...
hideto 评论 (1) 有 2239 人浏览 2011-05-26

Rails宝典之第六十四式: 自定义Helper模块

Rails默认为每个controller指定一个helper,所有的helper都放在app/helpers目录下 但是有些Helper我们希望是全局共享的,一般我们将这些Helper方法都扔在ApplicationHelper模块里 其实我们可以在app/helpers目录下建立我们自定义的Helper模块,如formatting_helper、path_helper等 # formatting ...
hideto 评论 (0) 有 1386 人浏览 2011-05-26

Rails宝典之第六十三式: model name in url

设计更利于搜索的url是SEO的一个要点,我们来看看怎样将http://localhost/products/6转化成 http://localhost/products/6-gallon-of-milk或者http://localhost/products/gallon-of-milk 对第一种url,首先添加permalink字段: create_table "products&q ...
hideto 评论 (0) 有 1056 人浏览 2011-05-26

Rails宝典之第六十二式: Hacking ActiveRecord

看下面一个场景: # product.rb class Product < ActiveRecord::Base validates_presence_of :price def self.find_ordered find(:all, : order => 'name') end end # product_test.rb require File.dirname(__FILE__) ...
hideto 评论 (0) 有 968 人浏览 2011-05-26

Rails宝典之第六十一式: Sending Email

这是一个使用Rails发送Email的简单指南 1,修改config/environments/development.rb,配置smtp config.action_mailer.raise_delivery_errors = true # set delivery method to :smtp, :sendmail or :test config.action_mailer.deliver ...
hideto 评论 (0) 有 1193 人浏览 2011-05-26

Rails宝典之第六十式: 不用fixtures的测试

严重依赖fixtures的测试会变得十分脆弱,并且很难维护。 让我们来看看怎样写不使用fixtures的测试。 看cart/line_item的例子: class Cart < ActiveRecord::Base has_many :line_items def total_weight line_items.to_s.sum(&:weight) end end class Li ...
hideto 评论 (0) 有 847 人浏览 2011-05-26

Rails宝典之第五十九式: optimistic locking

Rails里的Magic Column Names 当两个用户尝试同时更新同一record时,其中一个用户的更新将被覆盖,我们可以使用乐观锁来解决这个问题。 首先给Model添加一个称为lock_version的column; # migrations/011_add_products_lock_version.rb add_column :products, :lock_version, : ...
hideto 评论 (0) 有 1343 人浏览 2011-05-26

Rails宝典之第五十八式: 怎样写Generator

来看看怎样写自己的Generator吧 首先运行: ruby script/generate console中的输出可以看到Rails默认的generate: Installed Generators Builtin: controller, integration_test, mailer, migration, model, observer, plugin, resource, sca ...
hideto 评论 (0) 有 1859 人浏览 2011-05-26

Rails宝典之第五十七式: Select or Create

看一个选择Category或从text_field创建新Category的例子: <!-- views/products/_form.rhtml --> <p> <label for="product_category_id">Category:</lable><br/> <%= f.collection_sel ...
hideto 评论 (1) 有 1428 人浏览 2011-05-26

Rails宝典之第五十六式: Logger

Rails在controller里自带了logger,我们可以用来做debug: def show @cart = current_cart logger.debug "Hello world! #{@cart.to_yaml}" # debug, info, warn, error, fatal end 我们可以在environment.rb里配置Logger的消息格式: ...
hideto 评论 (0) 有 1178 人浏览 2011-05-26

知识库信息

最新评论

不能适应超过三层的的override,比如我有A,B,C三个模板,B在A的基础上添加自己的东西,C在B ...
mingliangfeng 评论了 Rails宝典之第八式: layout与content_for
讲的很清楚,赞
lixinso 评论了 ActionController::Resources + ActiveReso ...
这个插件的下载地址:https://github.com/rails/ssl_requirement
kaogua 评论了 每天一剂Rails良药之ssl_requirement
能评论吗???
refar 评论了 Rails宝典之第五十一式: will_paginate
<%= error_message_on "post", "ti ...
fcp6316 评论了 Rails宝典之第六十五式: Stopping spam
rake db:fixtures:load # Load fixtures into the cur ...
xu_ch 评论了 Rails宝典八十一式:Rails2.0之Fixtures尝 ...
[/b][i][/i][u][/u]引用[color=red][/color][size=medium ...
linjie_830914 评论了 Rails源码研究之ActionController:二,ro ...
并行工程环境的面向成本设计
libiun 评论了 Rails宝典八十五式:YAML配置文件
...
xu_ch 评论了 Rails宝典之第五十七式: Select or Create
你老好了,找的就是他了
xu_ch 评论了 Rails宝典之第七式: layout详解
Global site tag (gtag.js) - Google Analytics