子レコードが関連付けられている場合に、親レコードが削除されないようにするコードを書いています。
コードは次のとおりです。
class Customer < ActiveRecord::Base
attr_accessible :name, :number
has_many :customer_bills
before_destroy :check_for_bills
private
def check_for_bills
if customer_bills.count > 0
errors.add :base, "cannot delete customer while Bills exist"
return false
end
end
end
意見
<% if flash[:error] -%>
<p class='error'><%=h flash[:error] %></p>
<% end -%>
コントローラ
def destroy
..
flash[:error] = @customer.errors
..
end
しかし、コードは正常に動作しているのに、エラー メッセージが表示されませんか? 問題に見えるのは?どんなガイダンスも役に立ちます。