每天一剂Rails良药知识库


最近更新文章

每天一剂Rails良药之Extracting Test Fixtures From Live Data

Rails做单元测试时,我们自己手动去创建所有的fixtures会是个噩梦,有没有什么好的方案呢? 我们可以利用数据库里已有的数据,写一个rake tast来把数据库的数据复制出来到YAML文件里: CreateFixturesFromLiveData/lib/tasks/extract_fixtures.rake desc 'Create YAML test fixtures from data ...
hideto 评论 (0) 有 1137 人浏览 2011-05-26

每天一剂Rails良药之Creating Dynamic Test Fixtures

Rails的fixture文件在传递给YAML解析之前先用ERB解析,这样一来我们就可以使用Ruby代码动态生成测试数据,而不用一条数据一条数据的写了: <% 1.upto(50) do |number| %> child_post_<%= number %>: id: <%= number + 3 %> title: This is auto-generate ...
hideto 评论 (0) 有 907 人浏览 2011-05-26

每天一剂Rails良药之Manage a Static Site With Rails

对于静态站点我们可以利用Rails的cache来管理,如在controller中添加如下代码: after_filter {|c| c.cache_page} 这样将会对该controller的所有action作缓存 注意我们不要对UserProfile等页面做缓存,只对对所有用户一样的内容做缓存
hideto 评论 (0) 有 1218 人浏览 2011-05-26

每天一剂Rails良药之Write Code That Writes Code

很不幸我的2006-4-11版本的Rails Recipes的这章内容部分缺失 太晚了又没有找到更新的版本 记为not finished.
hideto 评论 (0) 有 1201 人浏览 2011-05-26

每天一剂Rails良药之Convert Your Sessions To ActiveRecord

我们看看config/environment.rb文件,其中有以下一段: # Use the database for sessions instead of the file system # (create the session table with 'rake db:sessions:create') # config.action_controller.session_store = : ...
hideto 评论 (0) 有 889 人浏览 2011-05-26

每天一剂Rails良药之Stud Out Authentication

登录和认证常常是我们Rails系统所必需的,但经常不是程序的核心功能 我们可以在ApplicationController里定义logged_in?方法: def logged_in? local_request? end helper_method :logged_in? 这样我们就可以在我们的Rails系统中的任何地方使用logged_in?方法 而且我们简单的用local_request? ...
hideto 评论 (0) 有 820 人浏览 2011-05-26

每天一剂Rails良药之Make your URLs Meaningful(and pretty)

这篇内容没什么价值,《Agile Rails》里的Routing Request一节讲述的比较详细了 ActionController::Routing::Routes.draw do |map| map.connect ':controller/service.wsdl', :action => 'wsd' map.connect ':controller/:action/:id' map ...
hideto 评论 (0) 有 1304 人浏览 2011-05-26

每天一剂Rails良药之Rendering CSV From Your Actions

有时候我们需要输出Comma Separated Values(CSV)等各种形式的输出来满足用户的需要: class ExportController < Application ontroller def orders content_type = if request.user_agent =~ /windows/i 'application/vnd.ms-excel' else 't ...
hideto 评论 (0) 有 1422 人浏览 2011-05-26

每天一剂Rails良药之Keep An Eye On Your Session Expiry

Rails的session默认为当用户关闭浏览器时终止 我们可以在config/environment.rb里设置它: CGI::Session.expire_after 1.month 这需要一个插件,具体session设置请参考http://wiki.rubyonrails.org/rails/pages/HowtoChangeSessionOptions 这不是今天我们讨论的重点 出于安 ...
hideto 评论 (0) 有 1765 人浏览 2011-05-26

每天一剂Rails良药之Cleaning Up Controllers with Post-back Actions

我们习惯与one-action-per-request,但是我们(特别是初学者)很容易被controller里的new(),create(),edit()和update()这些方法弄晕。 其实我们可以用一个方法来代替them all: def edit @recipe = Recipe.find_by_id(params[:id]) || Recipe.new if request.post? @ ...
hideto 评论 (0) 有 1415 人浏览 2011-05-26

每天一剂Rails良药之Role-Based Authorization

我们的系统往往并不只是靠登录这么简单来控制权限,今天我们来看看基于角色的授权 假设我们的系统已经建立了昨天的users表 1,migration class AddRolesAndRightsTables < ActiveRecord::Migration def self.up create_table :users_roles, :id => false do |t| t.col ...
hideto 评论 (0) 有 1412 人浏览 2011-05-26

每天一剂Rails良药之Authentication

今天我们来看看Rails怎么处理登录认证 虽然Rails有很多登录认证的插件,但是我们可以自己动手丰衣足食 1,db/migrate/001_add_user_table.rb class AddUserTable < ActiveRecord::Migration def self.up create_table :users do |t| t.column :username, :st ...
hideto 评论 (0) 有 1534 人浏览 2011-05-26

每天一剂Rails良药之Safely Use Models in Migrations

我们平时做Migrations时除了更改schema,还经常需要更改data 但我们以前的Migrations可能不工作,因为data之间可能有依赖关系 我们可以通过在Migrations里定义Model来解决该问题: class AddPositionToProducts < ActiveRecord::Migration class Product < ActiveRecord:: ...
hideto 评论 (0) 有 1023 人浏览 2011-05-26

每天一剂Rails良药之Make Dumb Data Smart with composed_of

ActiveRecord有一个composed_of()方法用来声明组件关系,如: class Person < ActiveRecord::Base composed_of :address, :class_name => "Address", :mapping => [%w(address_street street), %w(address_city c ...
hideto 评论 (0) 有 1527 人浏览 2011-05-26

每天一剂Rails良药之DRY Up Your ActiveRecord Code With Scoping

Rails真的是在搜肠刮肚挖空心思想方设法的给代码减肥瘦身,这次我们来看看with_scope方法 class PostsController < ApplicationController before_filter :scope_posts_to_user def show @posts = Post.find(:all) end def create @post = Post.crea ...
hideto 评论 (0) 有 1465 人浏览 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