摘要: 在使用update_attribute方法时,不走validation走validation的方法:createcreate!savesave!updateupdate_attributesupdate_attributes!跳过validation的方法:decrement!decrement_counterincrement!increment_countertoggle!touchupdate_allupdate_attributeupdate_columnupdate_counters 阅读全文
posted @ 2013-08-03 23:42 wangyuyu 阅读(343) 评论(0) 推荐(0) 编辑
摘要: validates_presence_of :login, :message => "用户名不能为空!" validates_length_of :login, :minimum => 4, :message => "用户名长度须为4到20位字母或数字!" validates_uniqueness_of :login,:case_sensitive => false, :message => "该用户名已存在!" validates_presence_of :password, :message => 阅读全文
posted @ 2013-08-03 23:38 wangyuyu 阅读(499) 评论(0) 推荐(0) 编辑
摘要: Time的常用函数时间对象. Time.now返回当前时间.1、Time.atTime.at(time[, usec])返回time所指时间的Time对象. time可以是Time对象,也可以是表示自起算时间以来的秒数的整数或浮点数.若浮点精度不够时,可以使用usec. 它将返回time +(usec/1000000)所表示的时间. 此时,time和usec都必需是整数.生成的Time对象将使用地方时的时区.2、Time.gm、Time.utcTime.gm(year[, mon[, day[, hour[, min[, sec[, usec]]]]]])Time.gm(sec, min,ho 阅读全文
posted @ 2013-08-03 23:36 wangyuyu 阅读(17042) 评论(0) 推荐(1) 编辑
摘要: 1. rails中类与对象与SQL中表与行的关系rails中提供了对象关系映射(ORM),将模型类映射至表,模型类的关联表名是类名小写后的复数形式,如类名Order,对应的表名为orders;若类名包含多个大写单词,则应在单词之间使用下划线划分表名,如类名为TaxAgency,对应的表名为taxagencies。rails中将模型类的对象对应于表中的行,对象的属性对应于表中的字段。例:Sql代码createtable users( id int auto_increment primary key, username varchar(20), password varchar(20));在mo 阅读全文
posted @ 2013-08-03 23:33 wangyuyu 阅读(969) 评论(0) 推荐(0) 编辑
摘要: 1、Rails的国际化根据特定的locale信息,提取相应的内容通过config/environment.rb,指定应用的转换文件 config.i18n.load_path +=Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')] #指定搜索的文件类型 config.i18n.default_locale =:zh #指定默认的文件在config/locales中创建zh文件,提供转换的方式2、validates用于校验validates_presence_of 确认属性值不为nil也不为空va 阅读全文
posted @ 2013-08-03 23:31 wangyuyu 阅读(573) 评论(0) 推荐(0) 编辑
摘要: strip_tags(html)Strips all HTML tags from the html, including comments. This usesthe html-scanner tokenizer and so its HTML parsing ability is limited bythat of html-scanner.Examples strip_tags("Strip these tags!") # => Strip these tags! strip_tags("Bold no more! See more here...&q 阅读全文
posted @ 2013-08-03 23:30 wangyuyu 阅读(795) 评论(0) 推荐(0) 编辑
摘要: 一般来说,通常使用input的field都会做一些filter的动作,避免被不怀好意之徒塞一些危险的HTML code(script等)进去搞破坏。在ROR中,我们在前面加一个h()(一般不用括号?不容易看到?)即可,h在ROR中起什么作用呢?他是html_escape的alias(别名),他会将所有的""变成<,&gt,比如:js 代码 会变成: <script>alert('a');</script>这样就完全做不了乱了。因为所有的tag都被搞掉了。这样太严格了,有时候我们需要 阅读全文
posted @ 2013-08-03 22:41 wangyuyu 阅读(844) 评论(0) 推荐(0) 编辑
摘要: image_tag(source, options={}) LinkReturns an HTML image tag for thesource. The source can be a full path or a file.OptionsYou can add HTML attributes using theoptions. The options supports three additionalkeys for convenience and conformance::alt - If no alt text is given, the file name part of thes 阅读全文
posted @ 2013-08-03 22:35 wangyuyu 阅读(1183) 评论(0) 推荐(0) 编辑
摘要: render 先上点搜集的常用方式[ruby] view plaincopyprint?render :action => "long_goal", :layout => "spectacular" render :partial => "person", :locals => { :name => "david" } render :template => "weblog/show", :locals => {:customer => Custo 阅读全文
posted @ 2013-08-03 22:32 wangyuyu 阅读(12329) 评论(0) 推荐(0) 编辑
摘要: x.指定端口启动rails项目ruby script/server webrick -p 3000------------------------------------------------- 0.创建一个项目 rials demoshop ------------------------------------------------- 1.用脚手架创建产品(控制 模型 显示 数据) script/generate scaffold product title:string description:text image_url:string ----------------------- 阅读全文
posted @ 2013-08-03 22:20 wangyuyu 阅读(840) 评论(0) 推荐(0) 编辑
摘要: 自己很初级,初级的不能再初级,所以初次接触rails的时候,对于里面的create,new,build等方法不是很了解,用的很混乱,导致经常出现不必要的bug,很苦恼,决定,总结一下,结合网上已有资源,深入理解下 向数据库插记录时经常会用到new, build, create。这三个方法很容易混淆 save:rails中的save其实是create_or_update,新建或修改记录!不一定是新建,切记 new :只是在内存中新建一个对象,操作数据库要调用save方法。 create = new + 执行sql。 build:与new基本相同,多用于一对多情况下。还有一个不同请看使用示例 .. 阅读全文
posted @ 2013-08-03 21:50 wangyuyu 阅读(5970) 评论(0) 推荐(0) 编辑