Wednesday, 15 July 2015

Rails 4.2 - dependent: :restrict_with_error - access errors -


: restrict_with_error causes an error to be added to the owner if there is an associated object

I have added the following to my code:

  class owner & lt; ActiveRecord :: is in base_me: things, dependent: restrict_with_error termination  

I understand when I remove things depend on owner Strive to strengthen> Then an error should be raised. I try to access errors in my show action in owners_controller , but they can not be found:

  def show @owner = Owner.find (params [: Id]) End of @ Owner.errors  

Update - Delete code

  Delete def @owner = Owner.find (params [id]) @ owner.destroy flash [: notice] = "owner successfully deleted" answer (end)  

Given your code ...

Delete DF @ Mother = Owner.Find (Perm [[ID]) @ owner.destroy flash [: Notice] = "Owner successfully" with reply (@ Producer) End DEIF Show @ Producer = Owner.Find (Param [: ID]) @ Owner. Errors End

At this point you are trying to reach the errors, nobody will be there.

Errors are temporary. They do not continue with the object, and they do not cross the requests, they are only present on the model that exists in the same request that produces errors.

The only point at which errors will be available in your code inside delete , after you @ owner. Call on destroy . They will never be available inside your show action.

  Delete DF @ Producer = Owner.Find (Paramus [: id]) @ owner.destroy # You must check out @ Flash for owner.errors here [: notice] = "Owner Successfully deleted "reply_man (@ user)  

No comments:

Post a Comment