不可以在 /news/new 的 view 裡面寫 form_for @news ~"~
2008年7月12日 星期六
2008年6月3日 星期二
operate existed tables in rails
Just need specify table name and primary key in Model. All attribution in tables will appear automatically.
class ModelName < ActiveRecord::Base
self.table_name = "table_name"
self.primary_key = "primary_key"
end
標籤:
programming,
rails,
ruby
2008年5月24日 星期六
tree implement in ruby
Just for fun, but it work well.
01 #!/usr/bin/env ruby
02 #
03 #
04
05
06 def tree(dirname, indent = " |")
07 puts dirname + "/" if indent == " |"
08 dir = Dir.open dirname
09 for f in dir
10 if f == "." || f == ".."
11 next
12 elsif File.directory? dirname+"/"+f
13 puts indent + "-" + f + "/"
14 tree( dirname+"/"+f, indent+" |")
15 else
16 puts indent + "-" + f
17 end
18 end
19 end
20
21 tree (ARGV[0]? ARGV[0] : ".")
標籤:
programming,
ruby
2008年5月23日 星期五
closure tip
closure is so fun.
well design for meta programming.
note 1:
pass yield values.
note 2:
Every object in array call the same function.
well design for meta programming.
note 1:
01 #!/usr/bin/env ruby
02
03 class Hash
04 def find_all
05 temp_hash = {}
06 each {|key,value| temp_hash[key] = value if yield(key,value) }
07 temp_hash
08 end
09 end
10
11 square = { 0 => 0, 1 => 1, 2 => 4, 3 => 9 }
12
13 new = square.find_all { |key,value| key > 1 }
14 puts new.inspect
pass yield values.
note 2:
01 #!/usr/bin/env ruby
02
03 class Person
04 attr_accessor :name
05 def initialize(name)
06 @name = name
07 end
08 end
09
10 def command(obj,method)
11 obj.each {|x| yield x.send(method)}
12 end
13
14 family = []
15
16 %w(shelling sherry appollo).each do |name|
17 family.push Person.new(name)
18 end
19
20 command family, :name do |x|
21 puts "hello, I am #{x}."
22 end
Every object in array call the same function.
標籤:
programming,
ruby
2008年4月25日 星期五
boring comparison
show complex data structure in perl and ruby
perl
ruby
perl
#!/usr/bin/env perl
use warnings;
use Data::Dumper;
$a = ["a", "b", "c", { "key" => "value" } ];
print Dumper $a;ruby
#!/usr/bin/env ruby
a = ["a", "b", "c", { "key" => "value" } ]
puts a.inspect
標籤:
perl,
programming,
ruby
2008年4月19日 星期六
has_many() function note
if has_many :order
order.delete_all just set foreign key null
order.destroy_all clear all sub record
order.delete_all just set foreign key null
order.destroy_all clear all sub record
標籤:
programming,
rails,
ruby
2008年4月18日 星期五
apache2 and mongrel cluster on debian
Enable apache2 proxy
Configure Proxy in a virtual host
/etc/apache2/site-enabled/default add
Then set mongrel cluster
Final, restart apache2 and it work.
$ sudo a2enmod proxy proxy_http proxy_balancerConfigure Proxy in a virtual host
/etc/apache2/site-enabled/default add
DocumentRoot "RailsApp/public"
# even only RailsApp also work
ProxyPass / balancer://localhost/
ProxyPassReverse / balancer://localhost/
< proxy balancer://localhost/ >
BalanceMember http://localhost:3000
BalanceMember http://localhost:3001
BalanceMember http://localhost:3002
< /proxy >Then set mongrel cluster
$ sudo gem install mongrel_cluster
$ cd RailsApp
$ mongrel_rails cluster::configure -e development -p 3000 -N 3
# write config/mongrel_cluster.yml
$ mongrel_rails cluster::startFinal, restart apache2 and it work.
2008年4月17日 星期四
rss feed in rails
Note for the Way to create Atom and RSS 2.0 feed.
Suppose that the model we want show in feed is Post
Then write the view.
app/view/feed/rss.rxml
app/view/feed/atom.rxml
app/view/layout/application.rhtml add
$ ./script/generate controller feedSuppose that the model we want show in feed is Post
class FeedController < ApplicationController
def rss
@rss = @@post
end
def atom
@atom = @@post
end
private
@@post = Post.find :all,
:order => "update_at", :limit => 10
endThen write the view.
app/view/feed/rss.rxml
01 xml.instruct! :xml, :version=>"1.0"
02 xml.rss(:version=>"2.0"){
03 xml.channel{
04 xml.title("The site title")
05 xml.link(url_for("/"))
06 xml.description("The site description")
07 xml.language('en-us')
08
09 @rss.each do |rss|
10 xml.item {
11 xml.title rss.title
12 xml.link ""
13 xml.description rss.content
14 xml.pubDate rss.update_at
15 xml.guid ""
16 xml.author rss.declarer
17 }
18 end
19 }
20 }app/view/feed/atom.rxml
01 xml.instruct! :xml, :version=>"1.0"
02 xml.feed "xmlns" => "http://www.w3.org/2005/Atom" do
03 xml.title("The site title")
04 xml.link(url_for "/")
05 @atom.each do |atom|
06 xml.entry {
07 xml.title(atom.title)
08 xml.summary(atom.content)
09 }
10 end
11 endapp/view/layout/application.rhtml add
<%= auto_discovery_link_tag :rss, { :controller => '/rss', :action => 'rss' }, { :title => "RSS 2.0" } %>
<%= auto_discovery_link_tag :rss, { :controller => '/rss', :action => 'atom' }, { :title => "Atom" } %>
標籤:
programming,
rails,
ruby
2008年4月15日 星期二
rails helper block
Write for memo.
To set a helper in the form
<% helper_name do %>
<div>blah</div>
<% end %>.
The definition is
By the way, partial is the way do the same thing once. if the outer html is used frequently, the way to define a helper as above will be better.
To set a helper in the form
<% helper_name do %>
<div>blah</div>
<% end %>.
The definition is
def helper_name(&block)
content = capture(&block)
concat("what we want write before block".block.binding)
concat(content,block.binding)
concat("what we want write after block",block.binding)
endBy the way, partial is the way do the same thing once. if the outer html is used frequently, the way to define a helper as above will be better.
標籤:
programming,
rails,
ruby
2008年4月13日 星期日
spakit
The rails plugin spakit means Single Page Application KIT. It can transform rails app into single page app but not modify app structure in large scale by loading content of other controller into primary controller.
The installation step is from handlino.com
now installation finished.
Before using it, spakit need a layout railsapp/app/view/layout/spakit.rhtml
And the layout application.rhtml should contain
<%= javascript_include_tag :defaults %> to include default js file.
Now we can use it happy. Add <div id="content"></div> into the view of primary controller, and set a spakit link: <%= spakit_link_to 'new person', :url => new_person_path %>, the content of the url will appear in the div#content.
Also two other helpers contain in spakit, spakit_form_for and spakit_form_tag
link1: spakit at rubyforge
link2: spakit at github
The installation step is from handlino.com
$ sudo gem install spakit
$ cd railsapp/vendor/plugins/
$ gem unpack spakit
$ mv spakit-version spakitnow installation finished.
Before using it, spakit need a layout railsapp/app/view/layout/spakit.rhtml
<%= flash[:notice] %>
<%= yield %>And the layout application.rhtml should contain
<%= javascript_include_tag :defaults %> to include default js file.
Now we can use it happy. Add <div id="content"></div> into the view of primary controller, and set a spakit link: <%= spakit_link_to 'new person', :url => new_person_path %>, the content of the url will appear in the div#content.
Also two other helpers contain in spakit, spakit_form_for and spakit_form_tag
link1: spakit at rubyforge
link2: spakit at github
annotate models
The plugin used for adding column annotate into model file is introduced in Ralls Bible. Write for note. the newer introduction at http://pragdave.pragprog.com/pragdave/2006/02/annotate_models.html. Just two step to use it.
1.script/plugin install http://repo.pragprog.com/svn/Public/plugins/annotate_models
2. rake annotate_models
And all model now with column annotate at file head.
1.script/plugin install http://repo.pragprog.com/svn/Public/plugins/annotate_models
2. rake annotate_models
And all model now with column annotate at file head.
2008年2月17日 星期日
the end of winter recess
這個寒假比預期的廢了很多,預定的事項,想讀的書,沒一件做完的,再來,三月一日就是研究所考試了,看著別人即將踏入考場,自己摸著鼻子甸甸自己剩下不到一年的時間儲備,真是難以說話。雖然沒有完成預定的事項,不過練習 perl/ruby 之餘還是寫了些 bot,cwbbot 是個簡單的 ruby 天氣預報機器人,提供他 twitter 的帳號和密碼,丟進 crontab,就可以每天自動取得明日溫度。
2008年2月16日 星期六
perl 和 ruby 的 twitter shell
twitter 最近紅透半邊天,perl 和 ruby 都有了 twitter api,現在還有 twitter shell,perl 的 twitter shell 利用 cpan 下 install Twitter::Shell 就可以得到 /usr/local/bin/twittershell, 使用方式為新增一個 yaml,如: touch ~/.twittershell.yml , 裡面填入
ruby 版本內建在 twitter 這個 gem 內, sudo gem install twitter 就可以得到 /usr/bin/twitter,直接在 shell 下 twitter 就可以得到幫助。不過 ruby 版本對 unicode 沒有支援,無法顯示中文,嘗試在 twitter gem 內的 twitter.rb 加入
username: yourtwitteraccount在 shell 下 twittershell -c ~/.twittershell.yml 就進入該 shell, help 可以得到幫助。
password: yourtwitterpassword
ruby 版本內建在 twitter 這個 gem 內, sudo gem install twitter 就可以得到 /usr/bin/twitter,直接在 shell 下 twitter 就可以得到幫助。不過 ruby 版本對 unicode 沒有支援,無法顯示中文,嘗試在 twitter gem 內的 twitter.rb 加入
require 'jcode',不過沒有效果,作者也表示暫時不會加入 unicode 支援。
$KCODE = 'u'
2008年2月15日 星期五
land mine in file name extension of ruby
和 perl 不同, ruby 的 third party module package, 也就是 gems, 副檔名和一般 script 都一樣是 ".rb", ruby 搜尋 module 的 path 似乎又包含了 ".", 所以如果使用的 script 檔名和 script 引用的 gems 內的檔名相同的話, 就會衝突 :/
標籤:
programming,
ruby
2007年12月29日 星期六
gem update on debian
日前升級了 debian 上的 gem,隨即發生無法使用的狀況,檢查之後發現,debian 內建的 rubygems 套件安裝位置在 /var/lib/gems/,而gem 原生的安裝位置在 /usr/lib/ruby/gems/,因此升級後會發生找不到正確 gems 的狀況。
因為 gems 的目錄配置方式不符合 FHS,debian 在 3.x 就一直遲遲不發佈 rubygems 套件,即使目前 4.x 發佈了,在目錄的相容性上問題也很多。
目前看來要安裝 gems 又不用通過官方安裝包條件的最好方法就是,先安裝內建的 rubygems,執行 gem update --system 之後,會得到一個 /usr/bin/gem1.8,然後移除 rubygems 和 libgems-ruby1.8,最後將 /usr/bin/gem1.8 重新命名成 /usr/bin/gem,就可以得到原生的 gem。
因為 gems 的目錄配置方式不符合 FHS,debian 在 3.x 就一直遲遲不發佈 rubygems 套件,即使目前 4.x 發佈了,在目錄的相容性上問題也很多。
目前看來要安裝 gems 又不用通過官方安裝包條件的最好方法就是,先安裝內建的 rubygems,執行 gem update --system 之後,會得到一個 /usr/bin/gem1.8,然後移除 rubygems 和 libgems-ruby1.8,最後將 /usr/bin/gem1.8 重新命名成 /usr/bin/gem,就可以得到原生的 gem。
2007年11月27日 星期二
example ruby
今天在我的 Debian 上面裝了 ruby 來嚐鮮,感覺相當有趣,就如同作者所希望的 ruby 這顆紅寶石給人輕快簡單的感覺,相較於 perl 這個穩重的珍珠,各有所擅。
要建制一個 Rail application 的環境需要下的指令有
這還會自動安裝 libreadline 等等 lib
其中
然後再下
就可安裝 Rails,不過還未包含 Mongrel 伺服器,等等再研究須不需要現在安裝
要建制一個 Rail application 的環境需要下的指令有
sudo aptitude install ruby ri rdoc irb rubygem
這還會自動安裝 libreadline 等等 lib
其中
- ri 可以用來查詢 module
- irb 是 interactive ruby
- rubygem 是 ruby 的 package management framework,安裝完後有 gem 指令可用
- rdoc 可以用來生成 doc
然後再下
sudo gem install rails --include-dependencies
就可安裝 Rails,不過還未包含 Mongrel 伺服器,等等再研究須不需要現在安裝
訂閱:
文章 (Atom)

