字串操作
"abcde".first(2) => "ab"
在 log 檔裡過濾敏感性的資料
有些資料不適宜被記錄在 log 裡,比如 user 的密碼,可以使用
filter_parameter_loggin "password"
把它寫在 ApplicationController 裡
def self.all_names
find(:all).collect(&:name)
end
可以取出某欄位(name)的所有值,傳回為陣列
User.collect{ |u| u.login}
這樣也行
projects.collect(&:name).collect(&:downcase)
把 name 欄位取出,並改為小寫
使用name_scope 傳遞hash型態的參數
# models/task.rb
def self.find_incomplete(options = {})
with_scope :find => options do
find_all_by_complete(false, :order => 'created_at DESC')
end
end
可以用
@tasks = @project.tasks.find_incomplete :limit => 20
呼叫
使用 ajax 更新資料
太常用了,把 ajax 更新資料的幾個重點寫下來
<%= link_to_remote "xxx", :url => {:controller => "yyy", :action => "zzz", :id => "不用講", :target_new => "回傳的表單位置", :target_create => "更新後的回傳位置" %>
這裡我定義了兩個參數,用來標明 ajax 控制下,頁面更新的部位。一個是更新(或新增)的編輯表單,一個是更新(不論是編輯或新增)後的資料呈現,的位置。一併送過去,可以在表單中,順便運用資料呈現的位置。當然,更新資料後,也要讓表單消失。
底下是表單的碼
<% form_for :article, :url => {:action => "create"} do |f| %>
<%= f.hidden_field :rule_id, :value => @rule_id %>
<%= f.hidden_field :account_id, :value => session[:user] %>
<%= f.hidden_field :parent_id, :value => @parent_id %>
<%= f.text_field :subject, :value => @subject %>
<%= f.text_area :body, :cols => "65", :rows=>"5" %>
<%= f.text_field :tagset %>
<%= submit_to_remote "article_create", "送出回應", :url => {:controller => "article", :action => "create", :target_new => @target_new, :target_create => @target_create} %>
<%= submit_to_remote "reply_create_cancel", "取消", :url => {:controller => "article", :action => "create_cancel", :target_new => @target_new} %> <% end %>
ajax 事件
使用 replace_html 回傳時,注意 li 與 ul 的組合關係。如果回傳的資料裡,li 的結構不正確,有某個很多人用的瀏覽器會給你嚴重的抗議....把你搞得灰頭土臉....
Table 10-3. The Ajax Callbacks
Callback Called When
:loading The remote document is being loaded by the browser.
:loaded The browser has finished loading the remote document.
:interactive The user can interact, even if the document has not finished loading.
:success The remote document has loaded and has a success HTTP Status code.
:failure The remote document has loaded but does not have a success HTTP Status code.
:complete The remote document has been completely loaded.
關聯性設定
class Forum < ActiveRecord::Base
has_many :topics, :dependent => :delete_all
has_many :posts, :through => :topics
end
class Topic < ActiveRecord::Base
belongs_to :forum, :counter_cache => true
belongs_to :user
has_many :posts, :dependent => :delete_all
end
class Post < ActiveRecord::Base
belongs_to :topic, :counter_cache => true
belongs_to :user, :counter_cache => true
end
question 裡有一個文字欄位,儲存 tag 字串。在建立時,會予以格式化。並一一在 tag 表格中建立標籤。
question_tag 表格則儲存每一問題所用到的每一個標籤。
@question = Question.find(:first,
:conditions => conditions,
:include => [:user, :group, {:question_tag => :tag}])
def create
...
if @forum.save
flash[:notice] = 'Forum was successfully created.'
format.html { redirect_to forums_path }
format.xml { head :created, :location => forum_path(@forum) }
else
...
end
:include => [:user, :group, {:question_tag => :tag}]) 同於
:include => [:user, :group, {:question_tag, :tag}])
但
:include => [:user, :group, [:question_tag, :tag]]) 不行
ps.不能設 has_many :tag, :through => :question_tag?? why