SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    NC
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help w/ errors.add_to_base between controller and model

    Hello,

    I'm having trouble in routing some errors between model and controller. The errors produced in the controller (invite_controller.rb) are collected and spit out nicely in a flash[:validate] method as such:

    Code:
    if errors.size > 0
       flash[:validation] = errors.join('<br />') 
    end
    Errors produced in the model (invite.rb) are displayed w/in a giant page failure explosion and are not routed into the flash[:validate] method.

    The code for the the method is as follows:

    Code:
    self.errors.add_to_base("an invitation has already been sent to #{email}")
    Is there a way where I can route the model errors to the flash[:validate] method as I am in the controller?

    Thanks for your help!

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think the flash is very good for storing errors. Instead of redirecting, you could use render :action => the_action;

    But anyway, you can find the errors of an object with @object.errors.full_messages.

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    NC
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know what the model error messages are and they do show when the form is submitted but the model errors are displayed w/in the context of other errors as such:

    Code:
    /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/validations.rb:748:in `save!'
    #{RAILS_ROOT}/app/controllers/invite_controller.rb:40:in `bulk'
    #{RAILS_ROOT}/app/controllers/invite_controller.rb:30:in `bulk'

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You probably have another error (not a validation error, but a bug). Please show us your code.

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    NC
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for getting back to me on this. I'm pretty sure it's a but but I can't seem to nail it down.

    Here's the Model code:

    Code:
    protected
    
    def validate   
      if new_record?
       dup = Invite.find :first, :conditions=> ['space_id = ? AND email = ? AND expire_at IS NOT NULL AND expire_at > CURRENT_TIMESTAMP', space_id, email]
           errors.add_to_base("an inviation has already been sent to #{email}") if dup
     end    
    end

    And here's the controller code:
    Code:
    def bulk
        @top_title = 'Bulk Invitations'
        @confirm_sent = false
        focus_on_load 'bulk_addresses'
        if request.post?
          @bulk_addresses = params[:bulk_addresses]
          errors = []
          invites = []
          addrs = params[:bulk_addresses].strip.split(/\s*,\s*/)  || []
          addrs.each do |addr|
            invite = create_invitation(addr, nil)    
            if invite.valid?
              invites << invite
              invite.save!
            else
              errors << "#{addr} is not a valid email address"
            end
          end                                        
          if invites.size > 50
            flash[:validation] = 'Only 50 invitations may be sent at a time'
          elsif errors.size > 0
            flash[:validation] = errors.join('<br />') 
          else
            invites.each do |invite|
              UserMailer.deliver_invite(logged_in_user, invite, rsvp_url(invite))
            end
            flash[:notice] = "#{addrs.size} invitations sent"
            @confirm_sent = true
          end
        end
      end
    
      def create_invitation(email_or_username, options = {})    
        
        logger.warn( "(eric) [create_invitation] email_or_username-->>#{email_or_username}")
        
        return nil unless logged_in_user.can_invite_to_space?
        invite = Invite.new(options)
        invite.email_or_username = email_or_username  
        invite.space = active_space 
        if invite.email && invite.space
           invite.subject = render_to_string(:partial=>'shared/templates/invite_subject', 
                                             :locals=>{:space=>active_space}) if invite.subject.blank?
           invite.body = render_to_string(:partial=>'shared/templates/invite_body', 
                                          :locals=>{ :space=>active_space,
                                                     :sender=>logged_in_user })  if invite.body.blank?
        end
        return invite
      end
    This is a project I just walked into and the code seems very strange. I do like the idea of keeping the validation in the model which I heard was usually the best practice.

    Thanks again for your time
    Last edited by clem_c_rock; Feb 8, 2007 at 01:25.


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
  •