1

子レコードが関連付けられている場合に、親レコードが削除されないようにするコードを書いています。

コードは次のとおりです。

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

しかし、コードは正常に動作しているのに、エラー メッセージが表示されませんか? 問題に見えるのは?どんなガイダンスも役に立ちます。

4

1 に答える 1

0

noticeRails 3.2では の代わりに使用する必要があると思いますflash

意見

<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>

上記のコードと適切なスタイルも試してみてください

CSS

#notice {
color: #000 !important;
border: 2px solid red;
padding: 1em;
margin-bottom: 2em;
background-color: #f0f0f0;
font: bold smaller sans-serif;
}

以前のようにエラーを追加できます

追加エラー

def ensure_not_referenced_by_any_line_item
  if line_items.empty?
    return true
  else
    errors.add(:base, 'Line Items present')
    return false
  end
end

私はレールを学んでいますが、上記が完璧な解決策であるかどうかはわかりません....

于 2012-10-17T05:50:35.873 に答える