rails
文章平均质量分 50
zimo1231
本人勤奋踏实,工作认真负责,自学能力强;性格开朗,容易与人相处,注重团队协作精神,且能承受较大压力。
展开
-
Rails Cookies与Session操作汇总
Rails 操作 session 和 cookies 非常简单,可以在 controller 与 view 中直接访问,model 除外,极大地方便了开发 Session#存信息session[:current_user_id] = user.id#取信息session[:current_user_id]#删除信息session[:current_user_id...原创 2014-07-07 12:17:45 · 246 阅读 · 0 评论 -
Deploy Rails App With Mina
原文请见我另外一个博客:http://caok1231.com/blog/2013/03/20/deploy-rails-app-with-mina/ capistrano是使用的最多的部署工具,最近社区中不少人开始推荐mina,试着用了下部署的速度的确快了很多。虽然mina最近两个月都没有更新,不过仍然希望这个项目能得到很好的发展。 下面简单总结下使用mina简单部署rails...原创 2013-03-27 09:06:41 · 133 阅读 · 0 评论 -
导入导出xml
controller中 def export_steps s = @part.steps.where('program_id in (?)', Program.checkable.map(&:id)) send_data s.to_xml(:only => [:position, :program_id, :name, :minute, :des...原创 2012-03-27 10:03:46 · 118 阅读 · 0 评论 -
使用link_to跳转到一个新的窗口
情况大概是我需要在Controller中处理一段URL,然后让用户在新窗口中打开这个URL但我知道redirect_to本身只是回传302,从而让浏览器打开新的地址,但是没办法在新的窗口中打开Google了一下,在Stackoverflow看到可以这样:view:<%= link_to "Buy this product", buy_path, :target=> "_...原创 2012-03-27 09:48:22 · 1468 阅读 · 0 评论 -
cap deploy中出现close for nilclass的错误
1.kirk并未启动,(使用cap deploy:start将其启动起来)2.查看kirkfile文件信息,确保其内容正确,且所有主为root/root3.kirkfile中若指定的端口为80时需要特别注意,(80端口需要root权限才可开启) ps -ef | grep kirkfile 查看该进程的所有主是否是root,否则80端口将无法开启4.查看当前kirkfile进程...2012-03-12 21:04:04 · 92 阅读 · 0 评论 -
rails post方式提交表单,session丢失的解决办法
rails3 里面 如果开启了protect_from_forgery。那么在提交的postData中就必须加入token这个字段来通过rails的后端验证, 提交的时候没有带token , 提交后session就会重置!解决方法是在页面加上:<%= csrf_meta_tag %> ...原创 2012-03-10 16:43:16 · 279 阅读 · 0 评论 -
Rails异常处理
ActionController::Base rescue_from ActionController::RoutingError, :with=>:render_404 private def render_404(exception = nil) if exception logger.info"Rendering 404: ...原创 2012-03-08 17:08:43 · 283 阅读 · 0 评论 -
Rails : new, build, create
首先说new吧,app = App.new(params[:app]),这个是用给定的params来实例化一个app对象,注意此时只是初始化了一个对象,ruby只是在内存里 面给app分配了一个内存地址,但是,这个地址还没有指定任何的有效的内存,所以接下来ruby要做的就是调用model里面的initialize函数 来根据给定的参数进行初始化这个对象,并将该对象保存到堆里面。...原创 2012-03-08 13:04:48 · 158 阅读 · 0 评论 -
rspec rails
rake spec rake spec:app包括plugins在内的specs: rake spec:all你也可单独运行 models、controller,view,helper或是plugin的specs: rake spec:models rake spec:controllers rake spec:views rake spec:helpers rake spec:plugin...原创 2012-03-07 16:04:00 · 104 阅读 · 0 评论 -
validates_numericality_of
在针对float类型的字段quantity进行如下数量限制时validates_numericality_of :quantity, :greater_than => 0semantic_form_for中= f.input :quantity, :step => 1会发现这样的错误A minimum value can not be determined when...原创 2012-07-09 11:09:58 · 218 阅读 · 0 评论 -
滚动公告功能的实现
1.新建一个表 create_table "notices", :force => true do |t| t.string "title" t.text "content" t.integer "user_id" t.date "expiration" t.datetime "created_at&quo原创 2012-06-26 19:44:10 · 456 阅读 · 0 评论 -
Deploy Rails App With Cloud Foundry
原文在我另外一个博客上,地址:http://caok1231.com/blog/2013/03/23/deploy-rails-app-with-cloud-foundry/这里直接拷贝过来的,就不在另外排版了。 Cloud Foundry是VMware于2011年4月12日推出的业界第一个开源PaaS云平台,它支持多种框架、语言、运行时环境、云平台及应用服务,使开发人员能够在几秒钟...原创 2013-03-27 09:25:21 · 212 阅读 · 0 评论 -
jQuery columnManager Plugin and Rails-settings-cached
原文参见我另一个博客:http://caok.github.com/blog/2012/08/07/jquery-columnmanager-plugin-and-rails-settings-cached/在平时页面显示表格时,容易出现要显示的字段过多,而在一页的范围内无法完全显示的困境。通过juery columnanager可以实现显示和隐藏的效果,再通过rails-settings-c...原创 2012-08-07 20:26:18 · 185 阅读 · 0 评论 -
rails中的时间显示格式【转】
在rails中需要显示时间时,格式化时间的方法一般选用strftime,下面是关于strftime的一些介绍strftime Format CodesYear Example%Yyear with century2010%yyear without century10%Ccentury number (year divide...原创 2013-02-26 12:21:40 · 207 阅读 · 0 评论 -
Caching With Rails
原文在我另一个博客上,这边就不在另外排版了,可参见http://caok1231.com/blog/2013/01/30/caching-with-rails/1.Page cachingPage caching是最简单最高效的一种,它会将Action最后的HTML结果存成public/下的HTML文件,也就是静态网页。class ProductsController < A...原创 2013-02-18 16:55:12 · 121 阅读 · 0 评论 -
VMC的使用
1.安装ruby环境rvm install 1.9.3rvm use 1.9.32.安装vmcgem install vmc --pre3.与Cloud Foundry建立连接vmc target api.cloudfoundry.com4.登录cloud foundryvmc login5.部署rails应用bundle packagebundle...原创 2013-01-16 13:04:51 · 1053 阅读 · 0 评论 -
ruby和rails的编程风格
Ruby 社区首推的代码编写风格原文: https://github.com/bbatsov/ruby-style-guide中文翻译:http://ruby-china.org/wiki/coding-styleRuby 社区首推的Rails代码编写风格 原文: https://github.com/JuanitoFatas/rails-style-guide...原创 2012-04-21 09:31:20 · 134 阅读 · 0 评论 -
Rails中获取url信息
Rails中获取url信息的一些方法如本机我测试域名为test.blog.zool.it:3000打开的uri为 /post/Hello-Worldfullurl为 http://test.blog.zool.it:3000/post/Hello-world则rails的路由生成一下几个方法 domain(tld_length = 1)取得域名request....原创 2012-09-06 14:40:24 · 108 阅读 · 0 评论 -
production环境下css和js文件未加载
production环境下css和js文件编译过,路径之类的都对,但浏览器缺未加载 在environments/production.rb中config.serve_static_assets = false原创 2012-08-25 10:01:52 · 190 阅读 · 0 评论 -
The Use of Google-Maps-for-Rails
http://caok.github.com/blog/2012/08/19/the-use-of-google-maps-for-rails/ 这个我前两天写的对个google maps for rails的使用的一个简单说明,之后也对里面的一些实用的东西进行补充。...原创 2012-08-22 09:16:04 · 117 阅读 · 0 评论 -
Rails with massive data
http://blog.xdite.net/posts/2012/08/22/rails-with-massive-data/ 关于用rails处理大量资料的,需要注意的一些建议。很实用的经验原创 2012-08-22 07:43:47 · 79 阅读 · 0 评论 -
rspec in rails
原文在我另外一个博客上,地址:http://caok1231.com/blog/2013/04/10/testing-rails-app/这里直接拷贝过来的,就不在另外排版了。 还有其他几篇关于rails测试摘录总结的博文地址:testing-rails-app#1testing-rails-app#2testing-rails-app#3testing-rail...原创 2013-05-04 16:33:57 · 115 阅读 · 0 评论 -
Factory Girl中的关系定义
原文在我另外一个博客上,地址:http://caok1231.com/blog/2013/05/02/factory-girl/这里直接拷贝过来的,就不在另外排版了。 很多时候我们都会使用 factory-girl 去构建测试数据,但如何去定义表之间的关系,特别是那种用户角色、权限之类的,接下来就稍微总结下如何用factory girl 去定义测试数据中的用户角色。...原创 2013-05-04 16:25:36 · 186 阅读 · 0 评论 -
A concise explanation of nil v. empty v. blank in Ruby on Rails
.nil?can be used on any object and is true if the object is nil.empty?can be used on strings, arrays and hashes and returns true if:String length == 0Array length == 0Hash length =...原创 2011-12-08 10:45:13 · 106 阅读 · 0 评论 -
rails中的常用命令
1.rails new demo2.cd demo3.rails generate controller Say hello goodbye生成一个控制器say和两个页面hello与goodbye4. rails server5.Rails g scaffold Product title:string description:text创建数据库中的表结构表名...2011-12-08 09:35:25 · 115 阅读 · 0 评论 -
joins与include的区别
:joins和:include的用法比较类似,但是二者又有一定的区别,这两个都能提高效率,让我们谈一下,他们的优点和区别吧 1>: 优点: 举个例子 accounts表和blogs表的关系是一对多 1)blogs = Blog.find(:all) # 查询一次blogs表 blogs.each {|blog| blo...原创 2012-01-05 12:39:22 · 177 阅读 · 0 评论 -
kaminari
Gemfile中gem 'kaminari' controller中@users = User.all.page(params[:page]).per(25) view中<%= paginate @users %> 具体参见:https://github.com/amatsuda/kaminari 显示序号,并且达到每页都累加的效果...原创 2011-12-30 13:59:19 · 110 阅读 · 0 评论 -
Ruby on Rails 表间关联
1 创建外键 t.integer : 表名单数 _id 关联表的命名要以首字母的顺序决定如 categories_products 关联表的联合索引: add_index:categories_products,[:product_id,:category_id]2 模型对象中指定关联 一对一关联 belon...原创 2011-12-28 10:38:05 · 384 阅读 · 0 评论 -
二级下拉选择框option_groups_from_collection_for_select
class Group < ActiveRecord::Base has_and_belongs_to_many :users # attribs: id, nameendclass User < ActiveRecord::Base has_and_belongs_to_many :groups # attribs: id, nameen...原创 2011-12-28 10:25:26 · 153 阅读 · 0 评论 -
has_and_belongs_to_many
users groups groups_usersididgroup_idnamenameuser_iddescriptiondescription 表groups_users只存在表结构,没有相应的model,controller,views等表groups_users的生成(该表没有id...2011-12-27 16:02:16 · 167 阅读 · 0 评论 -
Rubygem 常用命令整理
gem install gem-name # 安装gem,默认会安装最新版本gem uninstall gem-name #卸载gem,如果安装了多个版本会提示要卸载哪个版本gem cleanup #清理无用的gem,这个很有用。当你安装了很多版本的gem,想把旧版本的删除,一个个又太麻烦,就用这个命令gem list #列出所有安装的gemg...原创 2011-12-23 09:00:35 · 470 阅读 · 0 评论 -
提高rails new时bundle install运行速度
最近在新建rails项目时,rails new老是卡在bundle install那里,少则五分钟,多则几十分。这是因为rails new时自动会运行bundle install,而bundle install会自动去rubygems.org查找是否有更新。但需要的gems我早安装好了,且不用更新,所以上面步骤就可省了。强烈建议rails党委取消rails new时自动查找gems更新功能!...原创 2011-12-22 11:22:03 · 197 阅读 · 0 评论 -
rails区分view中的new和edit
在controller中@project的定义不同def edit @project = Project.find(params[:id])enddef new @project = Project.newendedit的时候 @project.id 为 true ,相应的action也就不一样,还会多生成一个input来标识put,因为此时是 update<...原创 2011-12-22 10:09:41 · 141 阅读 · 0 评论 -
ActiveRecord中处理find异常
当我们ActiveRecord中使用find的时候,如果表不存在或者字段错误了就会出现异常那么怎么捕获异常呢?用begin当然可以,比如下面这个helper中写的 def authenticate(userid, passwd) begin u = User.find(:first, :conditions=>["name = ? and passwd = ?", us...原创 2011-12-22 10:01:55 · 182 阅读 · 0 评论 -
rails 中 n秒页面自动跳转
关于n秒后跳转google上很多文章,讲的很详细了这里主要还是使用meta来实现直接照下面这样写就可以了render :text=>'<meta http-equiv="refresh" content="3; url=/welcome"> 3s refresh' 如果需要这里我们写个helper来做这个事情#创建一个helper>ruby script\gen...原创 2011-12-22 09:58:22 · 104 阅读 · 0 评论 -
批量删除记录
workhors/index.html.erb <% @workhours.each do |workhour| %> <tr class="<%= cycle("odd","even") %>"> <td><%= link_to format_id(workhour.id), workhour %&原创 2012-04-26 10:54:28 · 99 阅读 · 0 评论 -
Rubygems 镜像 - 淘宝网
由于国内网络原因(你懂的),导致 rubygems.org 存放在 Amazon S3 上面的资源文件间歇性连接失败。所以你会与遇到 gem install foo 或 bundle install 的时候半天没有响应,具体可以用 gem install rails -V 来查看执行过程。这是一个完整 rubygems.org 镜像,你可以用此代替官方版本,同步频率目前为...原创 2012-01-11 12:21:01 · 80 阅读 · 0 评论 -
send mail in rails
Creating and Sending Mail1.CREATE A Notice MAILERrails g mailer NoticeMailer decomp_changecreate app/mailers/notice_mailer.rbinvoke erbcreate app/views/notice_mailercreate a...原创 2012-10-22 14:48:48 · 188 阅读 · 0 评论 -
Ruby更改gem source
很多时候,在安装gem的过程中会出现找不到资源的error,我们需要从另外一个gem服务器下载安装。通过gem sources命令配置源,或通过修改Gemfile中的source语句可以实现。常用的源http://rubygems.org/http://gems.github.comhttp://gems.rubyforge.orghttp://ruby.taobao...原创 2012-06-13 10:54:31 · 87 阅读 · 0 评论 -
uninitialized constant Rake::DSL
使用Ruby 1.9.2,Rails 3.0.6版运行$ rake db:migrate时报错rake aborted!uninitialized constant Rake::DSL/usr/lib/ruby/1.9.1/rake.rb:2482:in `const_missing'/usr/lib/ruby/gems/1.9.1/gems/rake-0.9.0/li...原创 2011-12-03 15:01:07 · 122 阅读 · 0 评论