Display error message correctly

Asked

Viewed 650 times

1

I have this code:

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

and the system shows the following error message:

Unable to delete the record as it exists %{record} dependent

I want you to show it like this:

Unable to delete the record as it exists categories dependent

How should I do?

  • You tried to edit the file locales/*.yml?

  • Man, I could, but I need to use the same key for other models like equipment and etc.

1 answer

1

You must pass parameters to interpolation.

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

And to leave 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 are using validation dependent: :restrict_with_error in the models associations, Rails already generates the error message for you. To access it use:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.