我的视图中有一个 HTML 表,create, update and delete
用于显示名为Mast_Freq
.
该表的 被表和其他一些表Primary key
中的外键引用。所以,当我尝试删除一些记录时,我收到如下错误消息。'MastFreq'
'AssFreq'
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