每天一剂Rails良药知识库


未分类文章

Rails宝典之第二十三式: counter cache

请看Rails里的Magic Column Names 这次就是讲用_count字段来缓存has_many的计数 看Project和Task的例子: <h1>Projects</h1> <table> <% for project in @projects %> <tr> <td><%= link_to projec ...
hideto 评论 (0) 有 1602 人浏览 2011-05-26

Rails宝典之第二十二式: eager loading

看这个页面代码: <% for task in @tasks %> <%= link_to task.name, task_path(task) %> in <%= task.project.name %> <% end %> 上面的代码对每个task对象,取得project的name属性并显示,看看log: Project Lood (0.00 ...
hideto 评论 (0) 有 1412 人浏览 2011-05-26

Rails宝典之第二十一式: 超级简单的认证

续上一节。 我们知道Acts as Authenticated、restful_authentication等许多插件,我们这里为了开发方便也可以简单实现admin?方法: def admin? @current_user.name == "admin" end # or def admin? session[:password] == "foobar" ...
hideto 评论 (0) 有 1163 人浏览 2011-05-26

Rails宝典之第二十式: 限制访问

续上一节。 我们在页面中加上如下代码来限制public访问: <!-- episodes/index.rhtml --> <% if admin? %> <%= link_to 'New Episode', new_episode_path %> <% end %> 显然,只有admin才能新建Episode 我们来实现admin?方法 adm ...
hideto 评论 (0) 有 1005 人浏览 2011-05-26

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

我们通常对admin页面的做法是运行: ruby script/generate scaffold xxx 'admin/yyy' 其中xxx为我们的singular的Model名,而yyy为我们的plural的Model名 这样就相当于做了一套cms,后台页面和前台页面是两套东西 其实我们还有一种简单的admin方式,就是在前台页面直接加上管理的链接: <div id="e ...
hideto 评论 (0) 有 1488 人浏览 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) 有 1374 人浏览 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) 有 2465 人浏览 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) 有 1327 人浏览 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) 有 1780 人浏览 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) 有 1207 人浏览 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) 有 1501 人浏览 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) 有 1081 人浏览 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) 有 913 人浏览 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) 有 1281 人浏览 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) 有 1442 人浏览 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