2011-07-06 38 views
0

我的設置:Rails 3.0.9,Ruby 1.9.2確定始發控制器

我正在使用康康來授權控制器操作。如果用戶指定一個缺少ID,然後我有以下的代碼

application_controller.rb 

rescue_from ActiveRecord::RecordNotFound do |exception| 
    flash[:alert] = "Oops, I cannot find this record, please try again." 
     respond_to do |format| 
      format.html { redirect_to root_url } 
     end 
    end 

我想是上面的代碼在此設定閃光消息像

flash[:alert] = "Oops, I cannot find this person, please try again." 

「人」案例可以是任何模型,例如,如果用戶試圖訪問一個國家的缺少身份證,應該說

flash[:alert] = "Oops, I cannot find this country, please try again". 

你明白了。我想我應該能夠抓住始發呼叫和控制器,有誰知道如何做到這一點或有更好的方式來做到這一點?

回答

3

一兩件事你可以做的是分析其exception.message usally包含字符串看起來像這樣

「無法找到ID = 03 [WHERE images.state = '發表']圖片」

但我會用 params對象訪問控制器和動作導致錯誤

這樣的:

flash[:alert] = "Oops, I cannot find this #{params[:controller].upcase.singularize}, please try again." 

歡呼聲

+0

神奇,作品像魅力,謝謝。 – Bob