Display error message correctly

1

I have this code:

flash[:error] = t("errors.messages.restrict_dependent_destroy.many")

and the system displays the following error message:

  

Can not delete the record because there are % {record} dependents

I want it to show this way:

  

Can not delete the record because there are dependent categories

How do I do it?

    
asked by anonymous 13.05.2014 / 14:09

1 answer

1

You should pass parameters to interpolation .

    t("errors.messages.restrict_dependent_destroy.many", record: "categorias")

And to make generic you can get the name through the object:

    t("errors.messages.restrict_dependent_destroy.many", record: objeto_dependente.class.model_name.plural)

But if you're using dependent: :restrict_with_error validation in model associations, Rails already generates the error message for you. To access it use:

    flash[:error] = objeto_pai.errors[:base].join("\n")
    
15.05.2014 / 13:51