每天一剂Rails良药知识库


最近更新文章

Rails宝典之第十九式: admin在哪里

我们通常对admin页面的做法是运行: ruby script/generate scaffold xxx 'admin/yyy' 其中xxx为我们的singular的Model名,而yyy为我们的plural的Model名 这样就相当于做了一套cms,后台页面和前台页面是两套东西 其实我们还有一种简单的admin方式,就是在前台页面直接加上管理的链接: <div id="e ...
hideto 评论 (0) 有 1467 人浏览 2011-05-26

Rails宝典之第十八式: 循环flash

我们在application.rhtml(global layout)里经常需要写各种flash的显示: <% unless flash[:notice].nil? %> <div id="notice"><%= flash[:notice] %></div> <% end %> <% unless flash[ ...
hideto 评论 (0) 有 1348 人浏览 2011-05-26

Rails宝典之第十七式: 多对多Checkbox编辑

这次是讲多对多情况下的编辑,我们使用Checkbox来完成该工作: class Category < ActiveRecord::Base has_and_belongs_to_many :products end class Product < ActiveRecord::Base has_and_belongs_to_many :categories end 上面Category ...
hideto 评论 (0) 有 2448 人浏览 2011-05-26

Rails宝典之第十六式: 虚拟属性

看一个场景,用户注册时需要填写First Name,Last Name,Password: <h1>Register</h1> <% form_for :user, :url => users_path do |f| %> <p> First Name<br/> <%= f.text_field :first_name %&g ...
hideto 评论 (0) 有 1308 人浏览 2011-05-26

Rails宝典之第十五式: find条件

数据库查询的conditions除了简单的字符串,还可以用数组,range,nil等等,看看代码: Task.find(:all, :conditions => ["complete=? and priority=?", false, 3]) Task.find(:all, :conditions => ["complete=? and priority ...
hideto 评论 (0) 有 1758 人浏览 2011-05-26

Rails宝典之第十四式: Model上的calculations

没什么新意: >> Task.sum(:priority) => 15 >> Task.sum(:priority, :conditions => 'complete=0') => 13 >> Task.maximum(:priority) => 4 >> Task.minimum(:priority) => 1 > ...
hideto 评论 (0) 有 1192 人浏览 2011-05-26

Rails宝典之第十三式: Model放在Session里的危险

上次我们说到@current_user时不要将user对象放在session里,而只放user_id,然后每次去数据库取user对象。 有人不理解,这次的视频给出了答案: class UsersController < ApplicationController def prepare session[:user] = User.find(:first) redirect_to action ...
hideto 评论 (0) 有 1487 人浏览 2011-05-26

Rails宝典之第十二式: 重构用户名-p3

这次我们来重构我们的测试用例。 require File.dirname(__FILE__) + '/../test_helper' class UserTest < Test::Unit::TestCase fixtures :users def test_full_name_without_middle_initial user = User.new(:first_name => ...
hideto 评论 (0) 有 1064 人浏览 2011-05-26

Rails宝典之第十一式: 重构用户名-p2

继续重构。 我们先来写User类的单元测试,定义3个测试方法: require File.dirname(__FILE__) + '/../test_helper' class UserTest < Test::Unit::TestCase fixtures :users def test_full_name_without_middle_initial user = User.new(: ...
hideto 评论 (0) 有 895 人浏览 2011-05-26

Rails宝典之第十式: 重构用户名-p1

看这个显示用户Profile的页面: <h1>Profile</h1> <p> Name: <%= @user.first_name %> <%= "#{@user.middle_initial}." unless @user.middle_initial.nil? %> <%= @user.last_name ...
hideto 评论 (0) 有 1262 人浏览 2011-05-26

Rails宝典之第九式: 在日志里过滤敏感数据

这是个安全问题,当我们在系统注册页面输入密码等敏感数据时,我们可以看到,密码以明文的形式显示在日志文件里: Processing UsersController#create (for 127.0.0.1 at 2007-02-23 19:11:20) [POST] Session ID: 4047778b64af62d387f7e860e51cce20 [color=red]Parameters ...
hideto 评论 (0) 有 1424 人浏览 2011-05-26

Rails宝典之第八式: layout与content_for

如果我们想根据模板页面更改局部layout,使用content_for即可。 content_for允许模板页面代码放到layout中的任何位置。 比如我们的Rails程序不同的页面有不同的css样式,我们可以在layout里留出位置: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http ...
hideto 评论 (1) 有 2192 人浏览 2011-05-26

Rails宝典之第七式: layout详解

这次的视频很有用,详细解释了layout的用法 一般来说layout有如下五种: gobal layout,controller layout,shared layout,dynamic layout,action layout 假设我们有一个views/projects/index.rhtml页面: <h2>Projects</h2> <ul> <% ...
hideto 评论 (1) 有 2043 人浏览 2011-05-26

Rails宝典之第六式: Symbol to Proc

这次来介绍ActiveSupport里对Symbol的扩展,我们先来看active_support/core_ext/symbol.rb文件: class Symbol # Turns the symbol into a simple proc, which is especially useful for enumerations. Examples: # # # The same as peo ...
hideto 评论 (0) 有 1543 人浏览 2011-05-26

Rails宝典之第五式: 使用with_scope

这次来介绍with_scope方法的使用。 继续前面的例子,我们希望只取得complete为false的前20条数据,我们可以给find_incomplete方法添加一个Hash参数,然后使用with_scope将 额外的参数附加到我们的查询方法里: class Task < ActiveRecord::Base belongs_to :project def self.find_inco ...
hideto 评论 (0) 有 1357 人浏览 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