ror下有很多helper可以幫你生成tag
但對於習慣html語法的人實在是不習慣
好在仍有能使用一般html的方式
關於圖片
在ror下
若要取得public資料夾下的東西
只要在路徑上用 /即可
比方若public路徑下面有一個目錄pic下有一張圖bg.jpg
則url 設定為 /pic/bg.jpg即可
若要取得特定controller的action
則以
/controller/action的形式
比方若不想要用form_tag
則可在form標籤的
action設 "/controller/action"
一樣有類似的效果
2011年7月29日
2011年7月26日
RoR-建立關聯(1) has_one,has_many & belong_to
ror的model利用 has_many和belong_to來建立關聯
假設有兩個物件boyfriend 和 girlfriend
我們今天正常是一對一關係,所以
class boyfriend < ActiveRecord::Base
has_one :girlfriend
end
class girlfriend < ActiveRecord::Base
belong_to :boyfriend
end
假設今天來個花心男player
他對女性是1對多關係
我們可以這樣設
class player < ActiveRecord::Base
has_many:girlfriends
end
請注意ror對單複數的處理
has_one後面接單數
has_many後面接複數
當是1-1關係時,此時用的語法仍是has_one 與belong_to
一定有人會抗議,應該兩者都用belong_to或has_to
女人不是男人的附屬品,
其實呢
本來has_one 與belong_to的對應是與意化的
用途在決定外鍵的來源
foreign key便是,當A不存在時,B不應該存在
比方沒有班級就沒有學生
但男朋友死的 女朋友並沒有必要殉情
所以我們應該去砸雞蛋抗議嗎
其實ror沒那麼極端
參數 :dependent 可以決定一方消失 另一方何去何從
destroy=>相當於執行destory方法
delete=>delete但不執行destory 方法
nullify=>設為空 相當於 我沒男朋友
其差別可參考
http://hlee.iteye.com/blog/410000
延伸閱讀
http://ihower.tw/rails3/activerecord-relationships.html
假設有兩個物件boyfriend 和 girlfriend
我們今天正常是一對一關係,所以
class boyfriend < ActiveRecord::Base
has_one :girlfriend
end
class girlfriend < ActiveRecord::Base
belong_to :boyfriend
end
假設今天來個花心男player
他對女性是1對多關係
我們可以這樣設
class player < ActiveRecord::Base
has_many:girlfriends
end
請注意ror對單複數的處理
has_one後面接單數
has_many後面接複數
當是1-1關係時,此時用的語法仍是has_one 與belong_to
一定有人會抗議,應該兩者都用belong_to或has_to
女人不是男人的附屬品,
其實呢
本來has_one 與belong_to的對應是與意化的
用途在決定外鍵的來源
foreign key便是,當A不存在時,B不應該存在
比方沒有班級就沒有學生
但男朋友死的 女朋友並沒有必要殉情
所以我們應該去砸雞蛋抗議嗎
其實ror沒那麼極端
參數 :dependent 可以決定一方消失 另一方何去何從
destroy=>相當於執行destory方法
delete=>delete但不執行destory 方法
nullify=>設為空 相當於 我沒男朋友
其差別可參考
http://hlee.iteye.com/blog/410000
延伸閱讀
http://ihower.tw/rails3/activerecord-relationships.html
RoR-model的建立與資料庫的結合
在ROR中,實踐orm是利用model與資料庫結合
要利用model與資料庫結合,可以照以下步驟
1.建立與編輯modle
2.rake db:migrate RAILS_ENV=環境
注意:因為rail的慣例,model名稱必須為單數 而其migration 則為複數
ex:
model名為BLog
migration會對應到blogs
另外若是特殊的字如woman
還是會自動對應到women
1.建立model
利用rail g model Blog建立model
此時會自動生出相關文件
其中之一為 create_
2.編輯migration
會在db的migration下出現一個有create_blog名稱的檔案
用文字編輯器進去
會出現
class Blog < ActiveRecord::Migration
def self.up
create_table do |t|
t.timestamps
end
end
def self.down
end
end
加入
create_table blogs do |t|
t.string :name
t.string :content
t.timestamps
end
完成後打入
rake db:migrate RAILS_ENV=production
(如果是test環境則用test)
成功後即可在資料表用Blog.new直接操作
或
rails c
進到指令界面去操作
可以接觸資料庫後 就可以建立controller來讀取了
要利用model與資料庫結合,可以照以下步驟
1.建立與編輯modle
2.rake db:migrate RAILS_ENV=環境
注意:因為rail的慣例,model名稱必須為單數 而其migration 則為複數
ex:
model名為BLog
migration會對應到blogs
另外若是特殊的字如woman
還是會自動對應到women
1.建立model
利用rail g model Blog建立model
此時會自動生出相關文件
其中之一為 create_
2.編輯migration
會在db的migration下出現一個有create_blog名稱的檔案
用文字編輯器進去
會出現
class Blog < ActiveRecord::Migration
def self.up
create_table do |t|
t.timestamps
end
end
def self.down
end
end
加入
create_table blogs do |t|
t.string :name
t.string :content
t.timestamps
end
完成後打入
rake db:migrate RAILS_ENV=production
(如果是test環境則用test)
成功後即可在資料表用Blog.new直接操作
或
rails c
進到指令界面去操作
可以接觸資料庫後 就可以建立controller來讀取了
2011年7月25日
RoR -respond_to
respond_to 會根據使用者傳來的格式進行不同回應
格式為
respond_to do |format|
format.html {動作...}
format.xml {動作...}
format.附檔名3 {動作}
end
或有人有疑問format參數是怎麼來的,在系統預設他便是在路徑中傳遞
預設為.html
respond_to常用的搭配指令為
render 代表回傳訊息
redirect_to 代表導向頁面
因為render是系統預設回傳,在scaffold出來的程式,甚至可以看到
respond_to do |format|
format.html
end
的寫法,不是沒傳參數,而是此時系統會到預設的版模去抓(ror充滿這樣的預設)
如改成
format.html {render :text => "test"}
就會在顯示時出現
"test"
一般系統會輸出的格式html
要測試這樣的程式,不妨在視窗打入實際位址,比方
/msg/new.html
就會顯示出一般的畫面
msg/new.xml
就會顯示出系統對應的xml
比方輸入/msg/new.xml
會出現
<msg>
<name nil="true"/>
<created-at type="datetime" nil="true"/>
<updated-at type="datetime" nil="true"/>
<content nil="true"/>
</msg>
一些render能搭配的參數
如
:text #傳文字
:xml #傳xml
:json #傳json
:nothing #不傳
:template #樣版的檔名
:action #針對某個action預設的回傳頁面(不執行動作)
:status 傳200(正常) 404 這樣的訊息
有空不妨玩玩看
有空不妨玩玩看
訂閱:
意見 (Atom)