Resource Controller外掛程式使用

2021-08-24 17:18:43 字數 1736 閱讀 6557

rails生成的控制器中,大部分的action**都非常相似,每個action中基本上都有如下**:

respond_to do |format|

format.html # new.html.erb

format.xml

end

rails的宗旨是瘦controller胖model,現在controller還不算瘦,要再讓他瘦一點,那麼可以借助resource controller的力量,使用這個外掛程式可以減少大量controller重複的**。著名的電子商務程式spree [url] 就使用了這個外掛程式,讓控制器變動更精巧。

安裝resource controller有很多種方式。

在你的應用目錄下執行

ruby  script/plugin install git:

git clone git:

[url]

在控制器中有兩種方式使用,各有不同:

第一種繼承 resourcecontroller::base。

第二種在控制器中呼叫resource_controller方法。

可能你認為這麼做失去了對原來action的可控制性,那就錯了,resource controller可以讓你控制action的生命週期(before and after),下面的**應該能看明白:

class projectscontroller < resourcecontroller::base

new_action.before do #new action請求之前做些什麼?

3.times

endcreate.after do #create action之後做些什麼?

object.creator = current_user

endend

當然其它的action類似這樣的處理方式。

flash

class projectscontroller < resourcecontroller::base

create.flash "can you believe how easy it is to use resource_controller? neither could i!"

end

respond_to

增加響應型別

class projectscontroller < resourcecontroller::base

create.wants.js

end

比如要讓index方法響應輸出xml資料,可以這麼寫:

index.wants.xml

或者重寫

class projectscontroller < resourcecontroller::base

create.response do |wants|

wants.html

wants.js

endend

[url]

希望resource controller能為你帶來便利。

Grunt外掛程式autoprefixer使用指南

在grunt安裝 配置和應用中,我介紹了幾種外掛程式的使用。今天再介紹乙個為相容各大瀏覽器自動新增字首的外掛程式autoprefixer。環境配置好了後,gruntfile.js檔案配置如下 module.exports function grunt single file grunt.regist...

jquery highlight高亮外掛程式使用

jquery highlight官網 該外掛程式可以方便的進行高亮處理。示例 this is a test head input the search key.click 搜尋 button.this is a test body this is a test,a test,test,tes,te,...

NSIS KillProcDLL外掛程式 擴充套件使用

官網文件 解除安裝程式時,結束正在執行的應用程式 killprocdll killproc process name.exe 使用預設提供的killproc方法是強制結束程序,這可能導致意外的情況發生,例如資料丟失,程式崩潰等。為了防止上述的情況,我們需要向退出的應用發生指令,讓其完成退出所需的正常...