2008-07-01

rails魔术字段的实现 ,alias_method_chain用法

关键字: rails, alias_method_chain
Encapsulates the common pattern of: alias_method :foo_without_feature, :foo alias_method :foo, :foo_with_feature With this, you simply do: alias_method_chain :foo, :feature 替我们定义了两个方法:foo_with_feature 和 foo_without_feature <保存原来的foo方法> def foo_with_feature 在此添加想向foo方法里面写的代码 foo_with ...
1. 拷贝redcloth.rb到ror的lib目录内。 2. 拷贝jstoolbar相关的javascript 、stylesheet、images到对应的public目录里面。另外把压缩包里的help目录完整地拷贝到public目录下,其是redcloth语法的使用帮助. 3. 在application的helper方法中添加如下两个方法: require 'redcloth' #在application_helper 文件头引入redcloth 用于页中生成相应textarea框的jstoolbar方法。 def wikitoolbar_f ...
2008-06-26

rails route

关键字: url_for与路由
# ==== Relying on named routes # # If you instead of a hash pass a record (like an Active Record or Active Resource) as the options parameter, # you'll trigger the named route for that record. The lookup will happen on the name of the class. So passing # a Worksho ...
2008-06-25

ruby 日期

关键字: ruby time
difference = Time.now - time seconds = difference % 60 difference = (difference - seconds) / 60 minutes = difference % 60 difference = (difference - minutes) / 60 hours = difference % 24 difference = (difference - hours) / 24 days = differe ...
2008-05-22

使用include中嵌Hash取出一个多层次的对象关联数据

关键字: ruby ror model active_record
使用include中嵌Hash取出一个多层次的对象关联数据. 首先有如下关系: project issue : 一对多 issue comment : 一对多 comment history : 一对多 Project [1] <---- [n] issue (1) <---- (n) comment [1] <---- [n] history class Project has_many :issues end class Issue belongs_to :project has_many :cccc # c ...
2008-04-22

多对多关联数据存储ROR

关键字: ror 多对多关联保存 删除
Sku MODEL : class Sku < ActiveRecord::Base has_and_belongs_to_many :records, :delete_sql=>'DELETE FROM skus_records WHERE sku_id= \'#{id}\' AND record_id = #{record.id}', :before_remove=>:record_removed_info, :insert_sql =>'insert into skus_records (sku_id,record_id,cr ...
2008-04-09

HowToRunBackgroundJobsInRails--ap4r

关键字: ap4r ror ruby backgroundjobs rails 定期任务
http://ap4r.rubyforge.org/wiki/wiki.pl?GettingStarted 1. Business logics can be implemented as simple Web applications, or ruby code, whether it's called asynchronously or synchronously. 2. Asynchronous messaging is reliable by RDBMS persistence (now MySQL only) or file persistence, under t ...
使用的是一个一对多关联,代码如下:view: partial 名称:<input type="text" name="invoice[][name]" /> 描述:<input type="text" name="invoice[][description]" /><br/> rhtml: <% form_tag "/purchase/save_order" do -%> <p><label for="order_name">订单:</label><%= text_field :order, :name %></p> <p>物 品 ...
2008-04-03

render 页面javascript调用

关键字: ror render javascript javascript_function()
情景: 在使用 rail 中的 自动完成功能 auto_complete_field时,假若返回的页面中有 javascript脚本,那它将不会被返回。 必需把它用 javascript_function 方法进行改写。 a.rhtml 中 render _b.rhtml 页面。 在_b.rhtml中如果包含了 javascript脚本,则不能被render到a.rhtml中。 我在_b.rhtml中用了auto_complete_field来对用户输入进去提示。因为auto_complete_result 返回的已经是javascript脚本了,就不能把javascript发 ...
2008-03-25

统计一个字段中出现 ( 多个指定词) 的出现的次数

关键字: ror mysql auto_complete_field
mysql>select sum(if(locate(',watch,',concat(",",seokeyword,","))>0,1,0)) as watch,sum(if(locate(',fashion watch,',concat(',',seokeyword,',')>0,1,0)) as `fashion watch` from uxcell_channels; +-------+---------------+ | watch | fashion watch | +-------+---------------+ | 928 | 501 ...
2008-03-07

Single table inheritance:单表继承

关键字: ror single table inheritance
Single table inheritance:单表继承 转于:http://my4java.itpub.net/post/9983/78535 一、介绍: 关系数据库不支持继承,所以在将对象映射到数据库时,我们必须考虑如何在关系表中表现我们完美的继承结构。当映射到一个关系数据库时,我们试图最小化在多个表内处理一个继承体系时快速增长的结合。单表继承则将一个继承体系的所有类映射到单个表的字段中。 在一个关系数据库内至少可以有三种形式来表现继承体系。Martin Fowler 在http://www.martinfowler.com/eaaCatalog/中做了简短的描述。 1、Cl ...
2008-01-29

RJS Reference

关键字: ruby on rails, rjs
JavaScriptGenerator 摘自:《OReilly.RJS.Templates.for.Rails.Jun.2006.chm》chapter 7 The following is a list of all of the methods public methods offered by the JavaScriptGenerator. These methods are called on the page object in your RJS templates. Since RJS is all about generating JavaScript, it i ...
2008-01-16

ruby 语言 手记

关键字: mixin
Mixin 中的实例变量: 解决mixin多重继承中共享变量: 如下例,在不同的对象调用时,将可能得不到想要的结果。 module Observable def observers @observer_list ||= [] end def add_observer(obj) observers << obj end def notify_observers observers.each {|o| o.update} end end 下面的类使用Observable模块的一个类,但它自己也有 ...
2008-01-10

File io read/write to a file

关键字: file io read write
原本是想在以读写的方式打开一个文件,然后在此文件中查找相对应的字符并替换,起初偿试的是找到需要的字符后把文件的指针移动到此line的头位置,重写,然而重写的时候是replace而非insert,这样就导致了如果替换的字符小于或大于的时候,就会留下不能被替换掉。 f = File.open("/root/Desktop/test.txt","r+") c=f.readline => # "test file replace" len=17 假若需要把test字符换成 hello world 那么先把 f.seek(-17,IO:SEEK_CUR) 把文件指针移到test字 ...
2008-01-10

rake 单个与多个任务使用

关键字: rake task namespace
namespace :uxcell do desc "all rake many task" task :all => ["uxcell:u:a","uxcell:u:b","uxcell:u:c"] namespace :u do desc "all" task :outprint => ["uxcell:a","uxcell:b","uxcell:c"] desc "output a" task :a do puts 'a' end desc "output b" task :b do puts '','b','' end desc ...
2007-12-18

Create YAML test fixtures from database

关键字: rails ruby yaml fixtures test rake
转于 http://snippets.dzone.com/tag/yaml 运行: cd #{RAILS_ROOT}/lib/tasks/ rake db:fixtures:dump_all or rake db:fixtures:dump_references namespace :db do namespace :fixtures do desc 'Create YAML test fixtures from data in an existing database. Defaults to development database. Set ...
2007-12-18

rails 2.0 摘要

关键字: rails 2.0 升级 install
So how do I upgrade? If you want to move your application to Rails 2.0, you should first move it to Rails 1.2.6. That’ll include deprecation warnings for most everything we yanked out in 2.0. So if your application runs fine on 1.2.6 with no deprecation warnings, there’s a good chance that it’ll ru ...
1 。 在(ruby on rails) 的项目下面,确保public/目录中的 dispatch.fcgi , dispatch.cgi  ,  dispatch.rb 三个文件中          1: 确保第一行内容为本机的ruby的安装目录 。(例如:我本机的是  #!/usr/local/ruby/bin/ruby )             而在5.102上面, ...
 如何使用类名来获得类? 如果我有classname = "String时,如何生成SomeClass类的实例呢?主要有两个解决方法。" ruby 代码   [1] eval(classname).new   [2] Object.const_get(classname).new   第1种方法既简单又可以处理嵌套类(Net::HTTP等),但如果在CGI环境中滥用它的话,将十分危险。 而第2种方法却又无法处理嵌套类的问题。但如果进行以下 ...
 topic.to_xml(:skip_instruct => true, :except => [ :id, :bonus_time, :written_on, :replies_count ]) 更多方法参考API --> to_xml ActiveRecord::XmlSerialization @model[:pc] = ..... 可以賦 任何值。 哈希表里包含多层的话可以先把哈希表转为xml 格式,再把它 ...
2007-10-13

动态为class添加--属性和方法

关键字: ruby 属性 方法
ruby 代码   =begin       class Base         def add_xsor(xsor)           eval("class << self ; attr_accessor&nb ...
2007-10-08

REST_RUBY_XML

关键字: XML
  #response to client respond_to do |wants|     wants.html { redirect_to(person_list_url) }     wants.js     wants.xml  { render :xml => @person ...
2007-09-28

logger (ruby)

关键字: ruby>>logger
apache 项目中的与log4j对应的ruby版为->log4r ruby 代码 log = Logger.new(STDOUT) log.level = Logger::WARN log.debug("Created logger") log.info("Program started") log.warn("Nothing to do!") begin File.each_line(path) do |line| unless line =~ /^(\w+) = (. ...
字符串截取, 英文单词单数复数转换(Agile Web Development with Rails [Chapter 15||||||||||||||Active Support[P251]) ruby 代码 string = "Now is the time" puts string.at(2) #=> "w" puts string.from(8) #=> "he time" puts string.to(8) ...
xu_wccq
搜索本博客
我的相册
Ac7ad940-edbc-3b23-8443-5fe2ea338061-thumb
productcenter.bmp
共 2 张
最近加入圈子
存档
最新评论