1

I have problems displaying error_messages.html.erb partial when building custom validation.

I have for instance, a form to make a transaction, which starts like:

<%= form_for @transaction do |f| %>
  <%= render 'shared/error_messages', :object => f.object %>

I have a validation which prevents a user from making a transaction with himself:

validate :self_transaction_not_possible

with

    def self_transaction_not_possible
      bel = Belonging.find_by_id(self.belonging.id)
      unless bel.nil?
        if self.user_id == bel.user_id
          errors.add(:base, "You cannot perform a transaction with yourself")
        end
      end
    end 

Then I ran such an example in the browser (initiate a transaction on a belonging I own), and I placed a debugger inside self_transaction_not_possible. I checked that the validation happens, and indeed, inside the debugger, I see that typing: self.errors.full_messages give me my error message, so validation seems to be performed...

I would then have expected, as the object was not valid, that the error message would appear in my view, but no ... putting the debugger inside the error_messages.html.erb partial showed me that object.errors.any remains == {} :-/ ...

The user is then redirected towards the page it should see when the saving process fails (validation has been performed correctly), but he never sees the error message ...

Am I missing something obvious ? ...

2
  • It's hard to distinguish your typing errors from real bugs: <%= form_for @transaction, do |f| %> - what the comma is doing here? <%= render 'shared/error_messages'; :object => f.object %> - why did you put semicolon here? unless bel.nil ? - space after nil was intentional? And so on... Commented Sep 18, 2012 at 16:15
  • @jdoe: ok, I typed too fast and I couldn't do a copy/paste due to a network issue. Anyway, question is now typo-free (I hope)... Commented Sep 19, 2012 at 7:56

1 Answer 1

1

OK ... the reason it failed was because I was doing a REDIRECT_TO in the controller when the transaction was not being saved, thereby deleting the @transaction.errors vector ...

Changing this to a render :template => 'transactions/edit' solved my issue ..

Of course, it was hard to guess looking at the question :-/ ...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.