使用attachment_fu实现多图上传
请参考这篇文章。
(阅读更多精彩内容...)首先祝所有的朋友09年工作学习顺利。祝李猪猪小朋友即将到来的幼儿园生活开心。 刚才看了下respond_to这个方法,想写上一些东西。 最新做的项目,要求松耦合。因为之前2个月一直在做康盛的uchome的项目设计,对这种设计方式有点自己的想法。所以在初级设计这个代号kx2009的Rails项目时,走了些弯路。刚才看到redpond_to的文字,感觉又回到了一个正确使用Rails的方向,就是更加的REST,要web service。 REST的概念在我心里是来做资源管理的,这个资源管理要简单,所以我并不喜欢/users/1/posts/22这种资源嵌套,我更喜欢是/users/1,而他的posts就是/posts/index,/posts/22,足够。 多态。这次用到了很多多态的方法,使得设计上每个部分都很松耦合,能够复用,比如那个photos,就可以给活动,圈子,论坛去承担不同Model的图片上传,显示,管理功能。积分和信誉积分采用了同样的设计,下面的Account就是多态。(不过自动累加上一个Model的total字段稍微麻烦了点,magic有类似counter_cache的功能吗?对belongs_to那方的total字段进行自动加减?) 好了,看看Rdoc的描述。大意是:Rails是一个很好的web-service框架。 Without web-service support, an action which collects the data for displaying a list of people might look something like this: def index @people = Person.find(:all) end Here‘s the same action, with web-service support baked in: def index @people = Person.find(:all) respond_to do |format| format.html format.xml { render :xml [...]
(阅读更多精彩内容...)最近一个月在忙于开发项目,所以没有更新博客。不过开发中还是学到不少东西。 对于一个程序员,阅读是相当有帮助的,读别人的博客,读源码,读经验文章 ,即便读别人的开发代码,都是有帮助的。 本次开发的初始,就是在读别人的开发代码,然后一点点找到思路,继续在原有基础上加强功能。由于上一个版本的代码写的相当有水准,所以对自己的开发也提高不少。 本次开发的项目是一个在线视频教学系统。开发中用到了曾经写过的分页代码和一个rails插件:acts_as_taggable。 下面对这两个部分进行总结。 一、分页 下面的程序在3个项目上使用过。 models/page.rb class Page attr_accessor :pages #分页连接列表,[]类型 attr_accessor :cur #当前页 end helpers/application_helper.rb def howtopage_links(pagecls=nil) #在页面输出分页连接,pagecls:分页实例 if pagecls return pagecls.pages.collect {|t| [if t.to_s == pagecls.cur "<a class="now" href="http://railser.cn/wp-admin/?page=#%7Bt.to_s%7D">"+(t.to_s)+"</a>" else "<a href="http://railser.cn/wp-admin/?page=#%7Bt.to_s%7D">"+(t.to_s)+"</a>" end] } end end controllers/application.rb def howtopage(totalcount=0, per_page=6) cur_page = params[:page] || 1 #直接从页面取得page变量 x = (cur_page.to_i – [...]
(阅读更多精彩内容...)这是个很简单的Rails2.1.2的 find 的应用,来自Rdoc ActiveRecord::Base 一、find :first 和find :all 使用 Model.find_by_user_name ,来简写 Model.find(:first, :conditions=>["user_name=?",user_name]) 使用 Model.find_all_by_user_name ,来简写 Model.find(:all, :conditions=>["user_name=?",user_name]) 这里也可以使用and,比如典型的用户名和密码查询:Person.find_by_user_name_and_password(user_name, password) find也支持带参数的查询,比如Payment.find_all_by_amount(50, :order => “created_on”) 二、find_or_create_by_和find_or_initialize_ find_or_create_by_:当查询的内容不存在时,创建并保存该记录 find_or_initialize_:更上面的相似,只是创建不保存该记录,需要自己再次save 这里也支持参数和代码块,如 User.find_or_create_by_name(‘Bob’, :age => 40) { |u| u.admin = true } 三、find_by_的时候,支持多属性,如 Tag.find_or_create_by_name(:name => “rails”, :creator => current_user) 以上文字来自Rails2.1.2的Rdoc文档ActiveRecord::Base
(阅读更多精彩内容...)加载路径 标准的环境配置回自动将下列目录纳入应用程序的加载路径。 test/mocks/environment 由于该目录位于加载路径的第一位,在这里定义的类回自动覆盖真正的实现类,这样你就可以在测试阶段使用这些替代品。 app/controllers 目录及其子目录。 app/models 和 components 目录下所有以下划线或者小写字母开头的目录。 app, app/models, app/controllers, app/helps, app/services, app/apis, components, config, lib 以及 vendor 目录 检查 vendor/rails 目录,如果存在就从这里加载 Rails 库。 命名约定 变量名全部小写,单词之间以下划线分割。 类和模块的名称没有下划线,短语中每个单词的首字母大写。 表应该像变量一样,采用小写字母,并且是复数形式。 文件名全部小写,以下划线分割。 controller 用名词,action 用动词。当你创建了一个 action 的名字是 动词_名词 这样的格式,那么有必要再创建一个新的controller了 。
(阅读更多精彩内容...)第9章 使用局部模板。 render :partial => “cart”, :object => @cart render :partial => “cart_item”, :collection => @cart.items 辅助方法的一个示例。 # app/views/layout/stroe.rhtml “cart”) %> “cart”, :object => @cart %> <div> # app/controllers/store_controller.rb def hidden_div_if(condition, attributes = {}) if condition attributes["style"] = “display: none;” end attrs = tag_options(attributes.stringify_keys) ” ” end 第10章 validates_inclusion_of 方法验证某属性,在指定的列表中存在。防止别人构造不存在的支付方法逃避支付。 PAYMENT_TYPES = [ ["Check", "check"], [...]
(阅读更多精彩内容...)第12章 使用 :through 声明,可以通过间接关联来联系两张表。 可以使用 curl 或者 wget 工具来模拟请求xml。 class Product < ActiveRecord::Base has_many :orders, :through => :line_items end class Order < ActiveRecord::Base has_many :line_itemss end class LineItem < ActiveRecord::Base belongs_to :orders belongs_to :product end curl http://localhost:3000/info/who_bought/1 第11章 after_destroy 钩子方法于 delete 同在一个事务中,因此只要该方法里抛出异常,整个事务会回滚。after_destroy 会在 delete 语句执行之后被调用。 这里关键概念是,用异常来表示删除用户的过程中出现了错误。这里的异常同时承担两个任务。首先,在事务内部,异常会导致自动回滚;如果在删除用户之后 user 表为空,抛出异常可以撤销删除操作,恢复最后一个用户。 其次,异常可以把错误信息带回给控制器。 # app/model/user.rb def after_destroy if User.count.zero? [...]
(阅读更多精彩内容...)起因:在未做自己开发rspec的时候,只是调试下安装完 restful_authentication 的rspec和部分test时候,出现了几个错误: 1、 Spec::Mocks::MockExpectationError in ‘SessionsController Logging in by cookie fails cookie login with bad cookie’ fails cookie login with bad cookie(Spec::Rails::Example::ControllerExampleGroup::Subclass_1::Subclass_4) expected :cookies with (any args) once, but received it 0 times ./spec/controllers/authenticated_system_spec.rb:85: 2、 ‘SessionsController Logging in by cookie logs in with cookie’ FAILED expected true, got false ./spec/controllers/authenticated_system_spec.rb:81: 3、 ‘AccessControlTestController requesting xml; [...]
(阅读更多精彩内容...)一、环境 ruby -v >> ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32] rails -v >> Rails 2.1.2 二、需要安装的gem和plugin gem install ZenTest 因为我的Rails是2.1.2,所以在安装plugin时,用了下面的方法: ruby script/plugin install git://github.com/dchelimsky/rspec.git ruby script/plugin install git://github.com/dchelimsky/rspec-rails.git ruby script/generate rspec 另外还有一些辅助的工具,不在此详述 三、出现的问题 1、netbeans加载autotest时候的环境变量 解决:在系统环境中,需要加入home=当前项目的根文件夹,这样nb就能正常启用autotest了 2、restful_authentication的rspec时,出现mysql的Mysql::Error: Incorrect datetime value错误 mysql配置文件my.ini中,注释掉 sql-mode=”STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION” 一行(我的在87行) 下面添加一行:sql-mode=”" 重启mysql 四:参考文章 RubyTesting The Basics of Creating Rails Plugins,创建Rails插件的基础教程 RSpec的很重要的中文文档: http://www.letrails.cn/archives/20 http://www.letrails.cn/archives/advanced-rspec-tutorials-rspec-scaffold [...]
(阅读更多精彩内容...)感恩节放假,抓紧时间重读一下AWDWR把一些知识点记录如下。 第6章 迁移习惯 create 来创建表,add 给现有表增加字段。你可以会看到 002_add_price.rb 的迁移。 model 中验证方法设置为 protected 是因为该方法必须在特定的模型上下文中调用,不能在外部调用。 protected def validate errors.add(:price, “should be at least 0.01″) if price.nil? || price < 0.01 end errors.add() 方法第一个参数是字段名称,第二个参数是出错信息的正文。 在将价格和 0.01 比较之前,先检查它是不是 nil。试图将 nil 和数字比较会引发异常。 下面代码演示了如何用正则表达式验证模型属性。 validates_format_of :image_url, :with => %r{\.(gif|jpg|png)$}i, :message => “must be a URL for a GIF, JPG, or PNG image” [...]
(阅读更多精彩内容...)