我在我的查看中有一個HTML表,以create, update and delete
表的記錄爲Mast_Freq
。如何在Ruby on Rails中顯示ActiveRecord :: JDBCError的自定義錯誤消息?
該表的Primary key
被表'AssFreq'
和其他一些表中的外鍵'MastFreq'
引用。所以,當我嘗試刪除MastFreq
中的某條記錄時,我收到如下所示的錯誤消息。
ActiveRecord::JDBCError: [Sybase][JDBC Driver][SQL Anywhere]Primary key for row in table 'MastFreq' is referenced by foreign key 'MastFreq' in table 'AssFreq': DELETE FROM "MastFreq" WHERE "MastFreq"."Frequency_Code" = 'A'
如何向用戶顯示自定義錯誤消息而不是此錯誤消息。這個記錄不應該被刪除。
Frequency_Code
是表MastFreq
的主鍵。
Controller:
----------
class Asset::MastFreqsController < AssetController
rescue_from ActiveRecord::JDBCError, :with => :jdbc_error
def destroy
begin
@asset_master_frequency = Asset::MastFreq.find(params[:id])
result = @asset_master_frequency.destroy
respond_to do |format|
format.html{ redirect_to :action => :index}
format.json{ render :json => result}
end
rescue ActiveRecord::JDBCError
end
end
protected
def jdbc_error(exception)
flash[:error] = "You Cannot delete this Frequency Code" + exception.inspect
redirect_to asset_master_frequencies_path
end
end
一種方法是在呼叫周圍放置一個救援塊來刪除,並從中提出所需的自定義消息。 – mcfinnigan
是的,但請舉個例子。我是ROR的新手。仍在學習。謝謝。 – Vinay