国产一区二区精品久久_蜜桃狠狠狠狠狠狠狠狠狠_午夜视频精品_激情都市一区二区

當(dāng)前位置:首頁 > 網(wǎng)站舊欄目 > 學(xué)習(xí)園地 > 設(shè)計(jì)軟件教程 > 每天一劑Rails良藥之Cleaning Up Controllers with Post-back Actions

每天一劑Rails良藥之Cleaning Up Controllers with Post-back Actions
2010-01-14 07:04:48  作者:  來源:
我們習(xí)慣與one-action-per-request,但是我們(特別是初學(xué)者)很容易被controller里的new(),create(),edit()和update()這些方法弄暈。
其實(shí)我們可以用一個(gè)方法來代替them all:
Java代碼 復(fù)制代碼
  1. def edit   
  2.   @recipe = Recipe.find_by_id(params[:id]) || Recipe.new  
  3.   if request.post?   
  4.     @recipe.attributes = params[:recipe]   
  5.     redirect_to :main_url and return if @recipe.save   
  6.   end   
  7. end  

這證明了Ruby語言的強(qiáng)大和靈活
但和RESTful Rails Development不好合并,以為后者是對應(yīng)到上述不同的方法的

安徽新華電腦學(xué)校專業(yè)職業(yè)規(guī)劃師為你提供更多幫助【在線咨詢