Rails
文章平均质量分 67
dazhi_100
这个作者很懒,什么都没留下…
展开
-
不要让你的Rails Validate吓着了用户
ActiveRecord validations 是Rails很方便的一项特性,由于它如此好用,我们经常使用它,让用户看着了不舒服的信息,比如下面所说的例子:举个例子,比如现在我要在用户注册的时候验证 Social Security Number (SSN) ,一般代码会是这个样子的: Ruby代码 class User validates_prese转载 2013-07-15 17:24:54 · 2001 阅读 · 0 评论 -
rails 国际化
所谓的国际化:就是根据特定的locale信息,提取相应的字符串和其它一些东西(比如时间和货币的格式)等等。显然,有三个问题需要解决:1. 如何确定locale。2. 如何保存这些locale相关的字符串和其它信息。3. 如何根据locale提取字符串和其它相应的信息。让我们来看看rails如何处理国际化。(指rails2.2之后)首先看看第一点,如何确定locale可以转载 2013-07-15 19:01:18 · 1068 阅读 · 0 评论 -
诗歌rails之 关于Rails的错误提示 Rails flash error
首先,错误提示根据来源不同应该分Flash 和 error_message. 众所周知,flash应该是来源于controller,这个设计灵感来源是flash ram闪存,快速和暂时存储。稍微准确的定义是,保存信息直到下一次redirect。那么,这就会遇到问题,如果,我们没有redirect,而是render到另一个页面,那么flash提示就会一直存在。其实,Rails为fla转载 2013-07-15 21:07:07 · 882 阅读 · 0 评论 -
走和不走validation的方法
在使用update_attribute方法时,不走validation 走validation的方法:createcreate!savesave!updateupdate_attributesupdate_attributes!跳过validation的方法:decrement!decrement_counterincrement!increment_count转载 2013-07-15 17:23:13 · 632 阅读 · 0 评论 -
rails常用验证方法
rails常用验证方法validates_presence_of :login, :message => "用户名不能为空!"validates_length_of :login, :minimum => 4, :message => "用户名长度须为4到20位字母或数字!" validates_uniqueness_of :login转载 2013-07-15 17:23:44 · 1235 阅读 · 0 评论 -
如何 修改RAILS的默认错误提示 和 Rails validation error message 相关的合集
首先,有这样一个简单的问题,如果你想汉化,数字(不是长度)不能大于多少的model错误提示信息,怎么做?那么,先说验证是validates_numericality_of而数字大于多少的话,应该用:greater_than_or_equal_to 如果是希望了解,类似上面的信息的话,可以参考下面Active Record Validations and Callbacks而,我想说转载 2013-07-15 21:05:13 · 1663 阅读 · 0 评论 -
rake 出现问题的解决方式
Ruby:uninitialized constant ActiveSupport::Dependencies::Mutex (NameError)转载:http://hi.baidu.com/linuxerror/blog/item/f687603be6f5212871cf6cc3.html : uninitialized constant ActiveSupport::转载 2013-07-18 15:10:46 · 1433 阅读 · 0 评论 -
Linux下crontab命令的用法
任务调度的crond常驻命令crond 是linux用来定期执行程序的命令。当安装完成操作系统之后,默认便会启动此任务调度命令。crond命令每分锺会定期检查是否有要执行的工作,如果有要执行的工作便会自动执行该工作。而linux任务调度的工作主要分为以下两类:1、系统执行的工作:系统周期性所要执行的工作,如备份系统数据、清理缓存2、个人执行的工作:某个用户定期要做的工作,例如每隔10分转载 2013-07-17 18:54:21 · 403 阅读 · 0 评论 -
win7下搭载php+mysql+apach+netbeans
这学期开始学习php了,第一节课老师就教我们搭建环境,但是win xp下的通用环境。我是win7系统,所以就自己摸索着搭建了,其中还参考了许多网上资料,最后搭建成功。 我用的是最新的netbeans6.9.1 作为IDE,下面总结下我的环境搭建过程: 软件: Windows 7 旗舰版netbeans 6.9.1Apache 2.2转载 2013-07-17 21:14:17 · 1102 阅读 · 0 评论 -
window + ruby + rails + netbeans + mongrel
window下搭建Ruby OnRails平台1. 安装ruby,1.8.7版本win环境下有Ruby One-Click Installer包, 下载地址为http://www.ruby-lang.org/en/downloads/,下完后,点击安装,一直点next, i agree,选择安装路径,(我的安装目录是d:\ruby)然后一路next 最后fini原创 2013-07-18 21:57:19 · 749 阅读 · 0 评论 -
用光盘重装Linux系统 搭建rails平台
1. 电脑启动时,按住F10,直接进入DOS界面2. 将boot order中的CD-ROM改成首选项,并保存设置3. 电脑进入安装进程,将layout的语言选成English4. 联网:将bnac文件放在/usr/local/bin下,libcrypto.so.0.9.8放在/usr/lib下,执行脚本文件autorun-net.sh5.转载 2013-07-06 09:40:35 · 806 阅读 · 0 评论 -
rails 创建html中的image标签 image_tag
image_tag(source, options = {})Returns an html image tag for the source. The source canbe a full path or a file that exists in your public images directory.OptionsYou can add HTML attributes u转载 2013-07-30 20:51:51 · 5220 阅读 · 0 评论 -
rails脚手架
rails从2.0的版本开始,将scaffold的模块分离,作为一个项目的插件使用(每建一个项目,如果要使用这个scaffold的功能,就必须为这个项目下载这个插件)。脚手架scaffold可以快速实现基于数据表的相关增改查删的操作。如:我们建立一个网上电子图书的项目depot。 1.先用rails生成这个项目的框架 >rails depot 2.创建数据库 >mysql转载 2013-07-23 16:54:59 · 1949 阅读 · 0 评论 -
rails命令行命令
x.指定端口启动rails项目ruby script/server webrick -p 3000-------------------------------------------------0.创建一个项目rials demoshop-------------------------------------------------1.用脚手架创建产品(控制 模型 显示转载 2013-07-23 16:57:53 · 1318 阅读 · 0 评论 -
rails 过滤掉所有的html标签 strip_tags
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_ta转载 2013-07-30 20:58:05 · 2319 阅读 · 0 评论 -
ruby 截断truncate、单复数pluralize
截断runcate("Once upon a time in a world far far away") # => "Once upon a time in a world..." truncate("Once upon a time in a world far far away", :length => 17) # => "Once upon a ti..." tru转载 2013-07-30 20:45:39 · 2773 阅读 · 0 评论 -
rails 循环对象cycle
# Alternate CSS classes for even and odd numbers... @items = [1,2,3,4] @items.each do |item| %> class="even", "odd") -%>"> item end %> # Cycle CSS classes for rows, and t转载 2013-07-30 20:44:49 · 1636 阅读 · 0 评论 -
rails 将字符串的起始字母大写并将下划线转化为空格
humanize() 将开头字母大写,并将字符串中的下划线转换为空格Capitalizes the first word, turns underscores into spaces, and strips‘_id’. Liketitleize, this is meant forcreating pretty output. "employee_salary" # => "E转载 2013-07-31 20:51:16 · 1385 阅读 · 0 评论 -
rails 中的respond_to 对不同的文件要求给予相应
通过上一讲,我们已经对REST on Rails的基本框架有所了解,但是它是如何实现根据客户端的请求类型来返回不同类型的资源表示的呢?这就是我们这一讲所要讲的,秘密就在于respond_to。首先让我们来看看我们在上一讲中生成的airports控制器的代码:class AirportsController def index @airports = Airpo转载 2013-07-31 21:01:10 · 893 阅读 · 0 评论 -
Linux下搭建rails环境
一、重装Linux系统并搭建rails平台1. 电脑启动时,按住F10,直接进入DOS界面2. 将boot order中的CD-ROM改成首选项,并保存设置3. 电脑进入安装进程,将layout的语言选成English4. 联网:将bnac文件放在/usr/local/bin下,libcrypto.so.0.9.8放在/usr/lib下,先执原创 2013-07-25 20:49:53 · 1110 阅读 · 0 评论 -
rails中模型的关联关系 一对一、一对多、多对多及named_scope
1、1对1关系的实现 book与usermodel层 book.rb belongs_to:user user.rb has_one :book #体现两者之间的1对1关系 db/migrate层book.rb def self.up create_table :books do |t|原创 2013-07-25 20:58:44 · 4962 阅读 · 0 评论 -
rails 国际化、validate校验、flash提示
1、Rails的国际化 根据特定的locale信息,提取相应的内容通过config/environment.rb,指定应用的转换文件 config.i18n.load_path +=Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')] #指定搜索的文件类型 config.i18n.default_locale =:zh原创 2013-07-26 09:29:46 · 1834 阅读 · 0 评论 -
rails number_to_currency将数字转换为当前区域下的货币表示
number_to_currency(number, options = {})Link Instance Public methodsFormats a number into a currency string (e.g., $13.65). Youcan customize the format in theoptions hash.Options转载 2013-08-02 11:20:23 · 2545 阅读 · 0 评论 -
Rails中的增删改查
1. rails中类与对象与SQL中表与行的关系rails中提供了对象关系映射(ORM),将模型类映射至表,模型类的关联表名是类名小写后的复数形式,如类名Order,对应的表名为orders;若类名包含多个大写单词,则应在单词之间使用下划线划分表名,如类名为TaxAgency,对应的表名为taxagencies。rails中将模型类的对象对应于表中的行,对象的属性对应于表中的原创 2013-07-25 20:57:09 · 5716 阅读 · 0 评论 -
rails 多对多关系及多态
1、多对多关系并实现多态 学生、课程、老师 三者之间彼此为多对多的关系,谁上课体现了多态的性model层stu.rb has_many :summaries has_many :subjects, :through =>:summaries, :source => :subject #实现多对多关系 teacher.rb has_ma原创 2013-07-26 09:25:28 · 2421 阅读 · 0 评论 -
rails 复选框 check_box_tag
'product' %> Name Price for category in Category.find(:all) %> "product[category_ids][]", category.id, @product.categories.include?(category) %>转载 2013-08-01 18:44:45 · 4931 阅读 · 0 评论 -
rails image_tag生成图片标签
image_tag(source, options={})Link Returns an HTML image tag for thesource. Thesource can be a full path or a file.OptionsYou can add HTML attributes using theoptions. Theoptions suppor转载 2013-08-01 21:44:01 · 3278 阅读 · 0 评论 -
ROR中h()方法和sanitize的区别及Html Filter
一般来说,通常使用input的field都会做一些filter的动作,避免被不怀好意之徒塞一些危险的HTML code(script等)进去搞破坏。在ROR中,我们在前面加一个h()(一般不用括号?不容易看到?)即可,h在ROR中起什么作用呢?他是html_escape的alias(别名),他会将所有的""变成<;,>,比如:js 代码alert('a'); 会变转载 2013-08-01 21:53:24 · 830 阅读 · 0 评论 -
rails stylesheet_link_tag 创建一个html中的css标签
stylesheet_link_tag(*sources)Link Returns a stylesheet link tag for the sources specified as arguments. Ifyou don’t specify an extension,.css will be appendedautomatically. You can modify转载 2013-08-02 11:28:53 · 2297 阅读 · 0 评论 -
rails default_scope:为一个模型设置一个操作区域
default_scope(scope = nil)Link Use this macro in your model to set a default scope for all operations on the model.class Article ActiveRecord::Base default_scope { where(published: true)转载 2013-08-02 14:42:05 · 3112 阅读 · 0 评论 -
ruby protect_from_forgery ???????
class ApplicationController protect_from_forgery endrails2.0以后的版本都会默认开启该选项(在application_controller.rb中),它会利用保存在cookie中的 _csrf_token字段来生成自动添加在form中的隐藏字段_authenticity_token,然后利用 _authen转载 2013-08-02 15:39:04 · 1285 阅读 · 0 评论 -
ruby on rails的session
关于session总结。1.使用范围 controller和view中可用2.保存的数据 ◇一般保存会话进行的必要数据,如保存登录用户的的ID: user = User.find_by_name(params[:name]) session[:user_id] = user.id if user ◇尽量不要直接保存类实例到session里面。因为如果转载 2013-08-02 15:41:42 · 3747 阅读 · 0 评论 -
ruby 正则表达式
正则表达式是类型Regexp的对象。它们可以用显式的构造函数建立或者直接用 /pattern/ 和 %r/pattern/这种格式的字符常量构造。之后可以用通过使用 Regexp#match(aString) 或者用匹配操作符 =~(确定匹配)和 !~(否定匹配)与字符串进行比较。$&接受模式匹配成功的那部分字符。$`(键盘1左边那个键)接受模式匹配成功前面那一部分字符。$'接受模转载 2013-08-02 15:40:26 · 865 阅读 · 0 评论 -
Ruby will_paginate分页排序及显示
Ruby语言中,有一个叫做will_paginate的编程工具。那么对于这个工具的具体实现功能大家可能还不是很了解。下面我们就来详细讲解一下相关知识。正确理解Ruby更新文件全面剖析Ruby随机存取文件分享Ruby操作二进制文件技巧迅速掌握Ruby文件锁概念Ruby操作文件权限技巧分享新版本的Ruby will_paginate已经抛弃了基于plugin方式这样的做法,转而使用转载 2013-08-19 19:44:19 · 2013 阅读 · 0 评论 -
rails 的 Helpers
Action View:Helpers Measuring programming progress by lines of code is like measuring aircraft building progress by weight. - Bill Gates在Rails中, Helper 指的是可以在Template中使用的辅助方法,主要 用途是可 以将资料转载 2013-08-09 09:55:10 · 2115 阅读 · 1 评论 -
rails 之 form_for VS form_tag
下面介绍Ruby form的两种写法。1. Ruby form写法一:使用form_for { :action => :save_order } do |form| %> 40 %> 3, :cols => 40 %> 40 %> "submit" %>转载 2013-08-09 10:11:09 · 5034 阅读 · 0 评论 -
rails 常用的验证方法 validates
Agile Web Development with Rails17.4 validationvalidate 在save的时候激活validate_on_create createvalidate_on_update update通过这三个方法可以添加validates_XXX_xxx没有的功能错误信息写进er转载 2013-08-09 14:58:01 · 9458 阅读 · 0 评论 -
rails常用验证方法
validates_presence_of :login, :message => "用户名不能为空!"validates_length_of :login, :minimum => 4, :message => "用户名长度须为4到20位字母或数字!" validates_uniqueness_of :login,:case_sensit转载 2013-08-09 14:56:38 · 744 阅读 · 0 评论 -
rake aborted! undefined local variable or method `version_requirements' for #<Rails::GemDependency:0
rake aborted!undefined local variable or method `version_requirements' for #就:vi /home/httpd/redmine/config/environment.rb在开头加入:if Gem::VERSION >= "1.3.6" module Rails class转载 2013-08-15 15:34:09 · 1769 阅读 · 0 评论 -
Ruby:uninitialized constant ActiveSupport::Dependencies::Mutex (NameError)
Ruby:uninitialized constant ActiveSupport::Dependencies::Mutex (NameError)转载:http://hi.baidu.com/linuxerror/blog/item/f687603be6f5212871cf6cc3.html : uninitialized constant ActiveSupport::转载 2013-08-15 15:25:51 · 2032 阅读 · 0 评论