SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Zealot ricklach's Avatar
    Join Date
    Nov 2004
    Location
    Victoria BC
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Error/Validation messages fail to render

    When I customized my various forms that had been generated by scaffold I stopped getting error/validation messages. The necessary code is in place but the messages don't appear. Any ideas how to solve that problem?

    Rick

  2. #2
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ricklach
    The necessary code is in place but the messages don't appear.
    This is contradictory.
    Obviously the necessary code is not in place because it isn't working.

    Why not show us what's going on in the controller and view, for starters?
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  3. #3
    SitePoint Zealot ricklach's Avatar
    Join Date
    Nov 2004
    Location
    Victoria BC
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is the view for "new"
    Code:
    <% @header = "Add a New Person" %>
    <h2>Add a New Person</h2>
    <%= error_messages_for 'person' %>
    <%= start_form_tag :action => 'create' %>
    <table border="0">
      <%= render :partial => 'form' %>
    	<tr>
    		<td>&nbsp;</td>
    	</tr>
    	<tr>
    		<td align="center" colspan="2">
    			<%= submit_tag "Save" %>
    			<%= submit_tag "Cancel", {:type=>'button', :onClick=>"parent.location='" + url_for( :action=>'list' ) + "'" } %>
    		</td>
    	</tr>
    </table>
    <%= end_form_tag %>
    And here is the controller code for new and update:
    Code:
    def new
        @person = Person.new
        @residence = Residence.new
      end
    
      def create
        # will load an existing address if it exists, else create a new one
    	
    	residence = Residence.find_or_create_by_number_and_aptnum_and_postalcode(params[:residence][:number], params[:residence][:aptnum], params[:residence][:postalcode])
    	person = residence.person.build(params[:person])
    	if person.save and residence.save
    		flash[:notice] = 'The person was created successfully!'
    		redirect_to :action => 'list'
    	else
    		redirect_to :action => 'new'
    	end
     
      end
    and here is the model code:
    Code:
    class Person < ActiveRecord::Base
    	belongs_to :residence
    	validates_associated :residence
    	validates_presence_of :lastname, :firstname
    	validates_length_of :lastname, :maximum=>35, :message=>"Must be less than %d characters!"
    	validates_length_of :firstname, :maximum=>30, :message=>"Must be less than %d characters!"
    	#validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :on => :create
    	
    	
    end

    Does this help or do you need additional code?

    Rick

  4. #4
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Interesting, that does look like it should work.

    How about we see if the errors are coming through?

    Code:
    <%= @person.errors.full_messages.join("; ")  %>
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  5. #5
    SitePoint Zealot ricklach's Avatar
    Join Date
    Nov 2004
    Location
    Victoria BC
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I added that bit of code to the new.rhtml file and there was no difference when I executed the request (ie. no messages were given). What should I try next?

    Rick

  6. #6
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, I think I see it now.

    The @person object in the new action has no errors, it is new.

    If you were to render the new template instead of redirecting to the new action, it would probably be fine. No?
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  7. #7
    SitePoint Zealot ricklach's Avatar
    Join Date
    Nov 2004
    Location
    Victoria BC
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I changed the code to render and the application threw the following error: You have a nil object when you didn't expect it!
    You might have expected an instance of ActiveRecord::Base.
    The error occured while evaluating nil.errors

    My _form.rhtml code looks, in part, like this:
    Code:
    <tr>
    		<td align="right">First Name</td>
    		<td align="left"><%= text_field "person", "firstname", "size" => 30, "maxlength" => 30 %></td>
    	</tr>
    	<tr>
    		<td align="right">Middle Name</td>
    		<td align="left"><%= text_field "person", "middlename", "size" => 30, "maxlength" => 30 %></td>
    	</tr>
    	<tr>
    		<td align="right">Last Name</td>
    		<td align="left"><%= text_field "person", "lastname", "size" => 30, "maxlength" => 35 %></td>
    	</tr>
    The only difference between my code and the scaffold generated code is that the scaffold only has
    Code:
    <$= text_field "person, "firstname" %>
    and when the template is written that way it will render the error. The other problem is, of course, that the user entry is not being carried back to the "new" template to populate those fields that were already set but had no errors.

    I am open to further suggestions.

    Rick

  8. #8
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ricklach
    The only difference between my code and the scaffold generated code is that the scaffold only has
    Code:
    <$= text_field "person, "firstname" %>
    and when the template is written that way it will render the error.
    Are you saying that if you change your new template to include the code you indicate above, without changing the controller at all, errors will display properly?
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  9. #9
    SitePoint Zealot ricklach's Avatar
    Join Date
    Nov 2004
    Location
    Victoria BC
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not exactly. If I let scaffold generate the code the errors are displayed for person only. When I amend the scaffold code and add in the residence code the errors stop displaying. I tried several test but none of them worked. So I think that I will regenerate the code using scaffold and then add in the various changes to see just what is breaking the code - unless you have some other ideas.

    Rick

  10. #10
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ricklach
    Not exactly. If I let scaffold generate the code the errors are displayed for person only. When I amend the scaffold code and add in the residence code the errors stop displaying. I tried several test but none of them worked. So I think that I will regenerate the code using scaffold and then add in the various changes to see just what is breaking the code - unless you have some other ideas.

    Rick
    Yes.

    Note the difference between the way the scaffold create looks and yours, particularly the render vs. redirect.

    The error-filled @person is not being caried over to new on redirect, you are getting a brand new @person so there never are errors.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  11. #11
    SitePoint Zealot ricklach's Avatar
    Join Date
    Nov 2004
    Location
    Victoria BC
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did finally track down the differences between scaffold generated code and the code I had in my controller. Render vice redirect, as you suggested was one of the problems. The other solution was to put the "@" symbol in front of the variables and this seemed to solve the problem. Perhaps you can tell me why. However, errors from the linked table are not being added to the error display box but the actual elements on the form are being highlighted. So if you have any insight into this problem I would like to hear it.

    Rick

  12. #12
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've just had the same problem! Here is my solution:

    Use error_messages_for() to display the error messages. The name of the model needs to be passed to the method, so for my "contract" model I used:
    Code:
    error_messages_for(:contract)
    This needs to go in the form rhtml code. When I added it to the layout template, it created an error when not viewing the form.

    The messages are output into a DIV block with an ID and class "ErrorExplanation". You can use these to format the text in a CSS (assuming you've moved on from the scaffold CSS).
    Last edited by ReggieB; Mar 21, 2006 at 04:32.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •