ruby 1.9一些特性
hash key value
key:value
name:'wang'
status: :frozen
:不要與前面有空格
lambda語法
l = ->(arg) { puts arg }
l = lambda{|arg| puts arg}
1 ActiveRecord鏈式調(diào)用,scope
Task.where(status:3).joins(:inquire).offset(3).limit(3).order("tasks.inquire_state desc")
update_all 一個參數(shù),不接受條件
Task.where(status:3).update_all ""
Hash 條件
where(name:'he',age:(1..10)) #范圍
where(name:'he',age:[3,6,9]) #子集
Post.where.not(author: author) #非
named_scope -> scope
unscope 刪除所有作用域(包括default_scope)
2 coffeescript
縮進敏感的js小兄弟
turbolink 一半天使、一半魔鬼
如果你需要手工的排除一些鏈接,不讓Turbolinks幫你處理這些請求,你可以添加data-no-turbolnk屬性到鏈接標簽里面。
把js 和 css打包成一個文件,默認鏈接只更新body部分
$(document).ready每次新進的頁面(地址欄回車,跳轉)或者 刷新都會執(zhí)行。
$(document).on "page:change每次 Turbolinks 工作時執(zhí)行。
簡單的說,一般情況下ready 函數(shù)一般只執(zhí)行一次,而 page:change 在每次頁面切換時都會執(zhí)行。
$(document).ready -> alert "我在 Turbolinks 下只執(zhí)行一次。"
$(document).on "page:change", -> alert "我在 Turbolinks 下每次都執(zhí)行。"
$ ->
# 只會執(zhí)行一次,頁面切換不會再執(zhí)行
#some thing
#頁面切換
$(document).on "page:change", ->
#some thing
強烈不建議使用 turbolink,坑太多
3 sass
繼承,減少重寫樣式
rake assets:precompile
4 remote:true
link_to ,form_tag 等
rjs模版引擎
$("#list").html(j render partial: 'xxxxxx')
5 accepts_nested_attributes_for :author, :pages
接受嵌套屬性
class Contact
has_many :icc_phones
accepts_nested_attributes :icc_phones
end
Contact = Contact.new
params.require(:contact).permit(:name,:icc_phones_attributes:[:city_code,:phone_no,:_destroy])
= form_for @contact do |f|
name:
= f.text_field :name
= f.fields_for :icc_phones,f.object.icc_phones.first do |ff|
= f.fields_for :icc_phones,f.object.icc_phones.build do |ff|
city_code:
= ff.text_field :city_code
phone_no
= ff.text_field :phone_no
= ff.check_box :_destroy
6 routing
(get/post) 'profile', to: 'users#show'
match 'photos', to: 'photos#show', via: [:get, :post]# via: :all
root to: "home#index"
7 params
安全
params.require(:person).permit(:name,:age,..)
8 緩存
緩存html片段
俄羅斯套娃緩存 Russian Doll Caching
https://ruby-china.org/topics/21488
9 調(diào)試
bye_bug和 web_console
侵入式調(diào)試
byebug
c = continue
n = next
step
pry 可代替console