Sql injection是老问题,对如下查询:
def index
@tasks = Task.find(:all, :conditions => "name LIKE '%#{params[:query]}%'")
end
当用户输入的query条件加上单引号时很容易通过sql injection来攻击我们的Rails程序
而我们使用如下查询方式就可以避免sql注入问题:
def index
@tasks = Task.find(:all, :conditions => ["name LIKE ?", '%' + params[:query] + '%'])
e ...
这次讲的是一个textmate plugin textmate_footnotes,用来当Rails程序页面出错时可以点击Stack Trace链接去到
Rails程序源码甚至Rails源码中。
No use for no Mac guys.
-_-!
请看Rails里的Magic Column Names
这次就是讲用_count字段来缓存has_many的计数
看Project和Task的例子:
<h1>Projects</h1>
<table>
<% for project in @projects %>
<tr>
<td><%= link_to project.name, poject_path(project) %></td>
<td><small>(<%= pluralize project.tasks.size, 'tas ...
看这个页面代码:
<% for task in @tasks %>
<%= link_to task.name, task_path(task) %> in <%= task.project.name %>
<% end %>
上面的代码对每个task对象,取得project的name属性并显示,看看log:
Project Lood (0.000131) SELECT * FROM projects WHERE (projects.id = 330)
Project Lood (0.000156) SELECT * FROM projects ...
续上一节。
我们知道Acts as Authenticated、restful_authentication等许多插件,我们这里为了开发方便也可以简单实现admin?方法:
def admin?
@current_user.name == "admin"
end
# or
def admin?
session[:password] == "foobar"
end
# or ...
session[:password]的设置也很简单,即登录时设置session[:password]即可
续上一节。
我们在页面中加上如下代码来限制public访问:
<!-- episodes/index.rhtml -->
<% if admin? %>
<%= link_to 'New Episode', new_episode_path %>
<% end %>
显然,只有admin才能新建Episode
我们来实现admin?方法
admin?在我们的页面中使用,它是一个helper方法,但是我们希望在controller里也可以使用:
# controllers/application.rb
helper_method :admin? ...
我们通常对admin页面的做法是运行:
ruby script/generate scaffold xxx 'admin/yyy'
其中xxx为我们的singular的Model名,而yyy为我们的plural的Model名
这样就相当于做了一套cms,后台页面和前台页面是两套东西
其实我们还有一种简单的admin方式,就是在前台页面直接加上管理的链接:
<div id="episodes">
<% for episode in @episodes %>
<div class="episode">
<div class="side">
...
我们在application.rhtml(global layout)里经常需要写各种flash的显示:
<% unless flash[:notice].nil? %>
<div id="notice"><%= flash[:notice] %></div>
<% end %>
<% unless flash[:error].nil? %>
<div id="error"><%= flash[:error] %></div>
<% end %>
我们可以循环来输出flash:
<% flash ...
这次是讲多对多情况下的编辑,我们使用Checkbox来完成该工作:
class Category < ActiveRecord::Base
has_and_belongs_to_many :products
end
class Product < ActiveRecord::Base
has_and_belongs_to_many :categories
end
上面Category和Product是多对多的关系,下面我们来写Product编辑页面:
<%= error_messages_for 'product' %>
<p>
...
看一个场景,用户注册时需要填写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 %>
</p>
<p>
Last Name<br/>
<%= f.text_field :last_name %>
</p>
<p ...
Singleton是不能使用非Singleton的实例的
比如Spring中Manager是不能直接new Session实例来使用的
所以Spring用外部ThreadLocal的Session来提供给Manager透明的使用
不同Thread使用Singleton的Manager,而Manager对不同Thread使用新的Session实例
数据库查询的conditions除了简单的字符串,还可以用数组,range,nil等等,看看代码:
Task.find(:all, :conditions => ["complete=? and priority=?", false, 3])
Task.find(:all, :conditions => ["complete=? and priority IS ?", false, nil])
Task.find(:all, :conditions => ["complete=? and priority IN (?)", false, [1,3]])
Task.find(:all, ...
没什么新意:
>> Task.sum(:priority)
=> 15
>> Task.sum(:priority, :conditions => 'complete=0')
=> 13
>> Task.maximum(:priority)
=> 4
>> Task.minimum(:priority)
=> 1
>> Task.average(:priority)
=> 2.5
>> p = Project.find(:first)
=> #<Project:0x32dce38 @attrbutes={"name" => "Programming", "id" ...
上次我们说到@current_user时不要将user对象放在session里,而只放user_id,然后每次去数据库取user对象。
有人不理解,这次的视频给出了答案:
class UsersController < ApplicationController
def prepare
session[:user] = User.find(:first)
redirect_to action => 'show'
end
def show
@user = session[:user]
end
def update
...
这次我们来重构我们的测试用例。
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 => 'John', :last_name => 'Doe')
assert_equal 'John Doe', user.fu ...
继续重构。
我们先来写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(:first_name => 'John', :last_name => 'Doe')
assert_equal 'John Doe', user.full ...
看这个显示用户Profile的页面:
<h1>Profile</h1>
<p>
Name:
<%= @user.first_name %>
<%= "#{@user.middle_initial}." unless @user.middle_initial.nil? %>
<%= @user.last_name %>
</p>
<% link_to 'Users List', users_path %>
当别的页面需要显示用户名时,我们又得将上述代码copy一遍,敏感的开发人员可能已经闻到“风水”或bad ...
这是个安全问题,当我们在系统注册页面输入密码等敏感数据时,我们可以看到,密码以明文的形式显示在日志文件里:
Processing UsersController#create (for 127.0.0.1 at 2007-02-23 19:11:20) [POST]
Session ID: 4047778b64af62d387f7e860e51cce20
Parameters: {"user" => {"name" => "Ryan", "password_confirmation" => "abc123", "password" => "abc123"}, "commit" ...
如果我们想根据模板页面更改局部layout,使用content_for即可。
content_for允许模板页面代码放到layout中的任何位置。
比如我们的Rails程序不同的页面有不同的css样式,我们可以在layout里留出位置:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Todo List</title>
...
这次的视频很有用,详细解释了layout的用法
一般来说layout有如下五种:
gobal layout,controller layout,shared layout,dynamic layout,action layout
假设我们有一个views/projects/index.rhtml页面:
<h2>Projects</h2>
<ul>
<% for project in @projects %>
<li><%= project.name %></li>
<% end %>
</ul>
下面来看看各种lay ...
- 11:01
- 浏览 (2172)
- 论坛浏览 (2558)
- 评论 (3)
- 分类: Ruby
Ajax for Java developers: Write scalable Comet applications with Jetty and Direct Web Remoting
Create event-driven Web applications using Continuations and Reverse Ajax
Philip McCarthy(philmccarthy@gmail.com)
异步服务器端事件驱动的Ajax程序很难实现,也很难获得伸缩性。在作者的系列文章里,Plilip McCarthy展示了一个有效的方式:
Comet模式允许您push数据到客户端, ...
- 21:11
- 浏览 (2562)
- 论坛浏览 (3822)
- 评论 (8)
- 分类: Java
因为公司经常有PHP的外包项目做,客户指定使用PHP来开发,而我是Java组的,没做过PHP项目,心生好奇,趁今晚有空来摸摸PHP。
装了PHPeclipe和xampp,google到PHP手册。
一起就绪,学起PHP来,发现PHPeclipse的代码提示做的很不错。
可是慢慢看手册,我慢慢发现,都说PHP开发小型项目快,但是页面里嵌入一坨一坨的逻辑代码,非常不爽啊。
在看到MySql函数这章时,我已经完全受不了了!
<?php
// 连接,选择数据库
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_passwo ...
- 23:59
- 浏览 (13590)
- 评论 (57)
字符串和正则表达式与国际化关系紧密,utf-8可以让我们轻松搞定国际化问题
关于Unicode和utf-8入门,请看深入了解字符集和编码
Internally, a string is just a series of bytes.
在系统内部,字符串只是一个字节序列。
字符串本身不知道如何解析,它必须根据外部标准(编码)来决定如何解析。
一个字节不与一个字符一一对应。
Ruby有一个全局变量叫$KCODE,它有如下值:
a A ASCII
n N NONE (ASCII)
e E EUC
s S & ...
这次来介绍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 people.collect { |p| p.name }
# people.collect(&:name)
#
# # The same as peop ...
PHPeclipse是一个Eclipse插件,用来开发PHP程序
PHPeclipse安装很简单:
打开eclipse3.2, Help -> Software Updates -> Find and Install -> Search for new features to install -> New Remote Site -> http://phpeclipse.sourceforge.net/update/releases
xampp则是Apache,Mysql,PHP等软件的打包软件,方便了我们的PHP环境的安装
下载Windows下zip包,解压,修改apache/conf/ ...
- 13:36
- 浏览 (1935)
- 评论 (0)
这次来介绍with_scope方法的使用。
继续前面的例子,我们希望只取得complete为false的前20条数据,我们可以给find_incomplete方法添加一个Hash参数,然后使用with_scope将
额外的参数附加到我们的查询方法里:
class Task < ActiveRecord::Base
belongs_to :project
def self.find_incomplete(options = {})
with_scope :find => options do
find_all_by_complete(false ...
沿着Rails宝典之第三式: 通过关联做查询的脚步,我们可以进一步简化代码:
class Task < ActiveRecord::Base
belongs_to :project
def self.find_incomplete
find_all_by_complete(false, : order => 'created_at DESC')
end
end
class ProjectsController < ApplicationController
def show
@project = Project.find ...
每天一条Ruby小道之正则表达式
孟岩的《精通正则表达式》书评里讲到:
“正则表达式具有伟大技术发明的一切特点,它简单,优美,功能强大,妙用无穷”
其实很多语言都内置正则表达式支持,普遍的跟几乎所有手机都支持摄像头似的
好了,就让我们来领略Ruby下正则表达式的威力吧
1,正则表达式的表达形式
1)使用//分隔,如/Ruby/,/^abc/
2)使用%r加分隔符,如%r(xyz$),%r{[0-9]*},%r$[Rr]uby$
2,修饰符
1)/i忽略大小写
2)/m让“.”可以匹配换号符“\n”
3)/x允许正则表达式里有空格和注释
4)/o使得任何在正则表达式字面量中使用#{.. ...
Rails宝典之第三式: 通过关联做查询
我们来看一个has_many关联的例子:
class Project < ActiveRecord::Base
has_many :tasks
end
class Task < ActiveRecord::Base
belongs_to :project
end
class ProjectsController < ApplicationController
def show
@project = Project.find(params[:id])
@tasks = Task ...
- 15:01
- 浏览 (1383)
- 论坛浏览 (1418)
- 评论 (0)
- 分类: Ruby
- 浏览: 723087 次
- 性别:

- 来自: BJ

- 详细资料
搜索本博客
我的相册
screenshot
共 1 张
共 1 张
最近加入圈子
最新评论
-
Why OO sucks
看成去QQ SUCK 了
-- by xhanxhanxhan -
Rails的富文本编辑器插件 ...
有没有那个像textmate那种语法高亮的富文本编辑器?
-- by zllicho -
翻译www.djangobook.com之 ...
weiertzw 写道 1. >>> from django. ...
-- by chenjihua75 -
PHP、CakePHP哪凉快哪呆 ...
这孩子被java毒害太深。。。跳出java,你会发现外面的世界真的很大。
-- by woodless -
学习svn命令
只会用 apt-get 不是好孩子。
-- by smartly






评论排行榜