2007年11月27日 星期二

Globalize plugin

Get on Rails wit Globalize

修改environment.rb 記得要

restart application


不是 web server

2007年11月16日 星期五

REST

參考了許多人家的理解
REST簡介
DHH的演講翻譯
學習REST

目前自己的理解是 Web上的東西都是Resource, Resource 對HTTP的不同動作(CRUD -- Create,Read,Update和Delete), 該有不同的反應

2007年11月13日 星期二

h() , Time.now() and link_to()

h() method prevents the special characters in the e-mail address or ...

Time.now()
@time = Time.now
<%= @time %>

link_to()
<%= link_to "Goodbye", :action => "goodbye" %>

---------------------------------------------------------------------------
In controller:
@files = Dir.glob('*' )

In view:
<% for file in @files %>
 file name is <%= file %>
<% end %>

2007年11月12日 星期一

<%= ... %> , <% ... %> and <% ... -%>

<%= ... %> 內容會輸出
<% ... %> 內容不會輸出
<% ... -%> -號會把預設的空白行去掉

在rails 1.2.3 在ruby 1.8.6測試已經都不會有預設的空白行了

Sample1
<% 3.times do -%>
hi
<% end -%>
over

hi
hi
hi
over

Sample2
<% 3.downto(1) do |count| -%>
 <%= count %> ... <br/> <br/>才有真正換行 <%=%> 沒有
<% end -%>
kick off

3 ...
2 ...
1 ...
kick off

2007年11月11日 星期日

Controller View and URL

禮拜日好懶 ==
demo>ruby script/generate controller Say

2007年11月10日 星期六

Installing Rails

在windows 直接用installRails安裝

查詢版本
ruby -v, rails -v


更新版本
C:\rails_apps> gem update rails --include-dependencies
C:\rails_apps> gem cleanup
app> rake rails:update


Rails和database的階層關係
  • Ruby Programming
  • Ruby database adapter , Each database library have its own database-specific API, Ruby database adapter hide these differences
  • Database driver , Ruby library connect low-level database API to the high-level world of ruby programming
  • Database engine

2007年11月9日 星期五

Action Pack:The View and Controller

The controller supplies data to the view, and the controller receives back events from the pages generated by the views
The interactions , support for views and controllers in Rails is bundled into a single component , Action Pack


View Support
view 中的一些動態內容 大都是由controller的action由model取來的資料運用產生的
而Rails支援動態產生內容的格式有 rhtml, rxml, rjs ,這些格式在Rails被稱為template
  • rhtml:利用ERB的Ruby工具(embedded ruby)將 Ruby的code嵌入HTML的code中
  • rxml :可用Ruby的code建立XML文件, 產生的XML會自動符合code架構
  • rjs :可在Server端建立Javascript,在browser端執行 ,也就是用來做Ajax的

Controller
Controller是全部的邏輯中心, control user, view and model 之間的互動, 該去哪該做啥,做完給誰 ,而底層的互動已經被Rails做掉了


Controller除了之間的互動 還有
  • 把外部的request , URL指引到內部的action
  • cache : for performance
  • helper module for 擴增view的功能 ,又讓view本身的程式碼不會變大,就是可以多一堆plug-in的function的感覺
  • session : giving users the impression of an ongoing interaction with our applications???

2007年11月8日 星期四

Active Record: Rails Model Support

Object/Relational Mapping
ORM 把datebase table mapping 到class。如果有database table叫orders, 那就有相對應個class叫做Order. tables correspond to classes. rows in a table correspond to objects of the class


  • 透過ORM,把database整個物件化,讓我們不用直接存取 database data

example:
order = Order.find(1)
puts "Order #{order.custom_id}, amount=#{order.amount}"

Order.find(:all, :conditions => "name='lake'") do |order|
 puts order.amount
 order.discount = 0.5
 order.save
end


  • ActiveRecord就是Rails用的ORM層
example:
require 'active_record'
class Order <>

2007年11月7日 星期三

Models, Views, and Controllers

  • modal 用來存資料的 ,長時間的資料庫,短時間的cache都是存這,除了單純放資料外,還可以對資料加上基本的存取規則,避免程式上邏輯錯誤造成的資料存取。
  • view 顯示使用者介面,透過model可讀取資料,讀出的資料,靠view顯示,也只負責顯示,所以有可能好幾個view同時存取同一個model在不同情況下讀取相同資料做不同的顯示
  • controller 接收使用者指令 , 選擇對應的method給model 再把相對應的資料給view


  • Rails 強迫把程式分成controller,view,and model三個部份,然後再照Rails預設規則去跑.這就是應該所謂的 convention over configuration

2007年11月5日 星期一

NetBeans 的使用

我有用的快捷鍵
雖然官方的NetBeans講了很多快捷鍵的功能, 可是我只有試出幾種, 可能也是我沒仔細去看吧
  • Ctrl + Alt +Space 或 Ctrl + \ : Auto Complete
  • Ctrl + Tab : Change Tab
  • Alt + Enter : How to use Surround With
  • Ctrl + mouse Left Key 去點method 會跳到去宣告的地方
  • Tools -> Options -> Keymap 可以設定自己習慣的快捷鍵
  • re + tab:<%=%>
  • r + tab:<%%>
  • ctrl + g:快速定位
  • ctrl + k:自動完成剩下的程式碼

如何讓NetBeans的debug 功能可以用

1. D:\InstallRails\rails_apps>gem install ruby-debug-ide

2. Change tool -> Options -> Ruby interpreter to D:\InstantRails\ruby\bin\ruby.exe