Ruby on Rails Friday, February 25, 2011

Yes it is in the controller, I will move it when I get it working.  How do I get validations on both?

On Friday, February 25, 2011 10:32:54 PM UTC-6, bacrossland wrote:

Because that is the top most error. The others are ignored until that error is fixed.

What are you trying to achieve with this error handling? It looks like you are looking to do validation on the information that is passed in. This code also looks like it is your controller. Is that correct?

B.

On Fri, Feb 25, 2011 at 10:27 PM, Me <chab...@gmail.com> wrote:
It is only catching the first rescue one when I know other validations are failing.


On Friday, February 25, 2011 10:20:35 PM UTC-6, bacrossland wrote:
It looks fine but you have to run it to make sure it catches what you want it too.

B.

On Fri, Feb 25, 2011 at 9:45 PM, Me <cha...@gmail.com> wrote:
So is this the correct way:

def create
    bad =[]
    begin

      @tour_type = TourType.new(params[:tour_type])
      if @tour_type.save
       render :update do |page|
         page << "Redbox.close;"
         page.call "ProtoGrowl.success", "Succesfully created #{@tour_type.name}"
       end
      end
      rescue ActiveRecord::MultiparameterAssignmentErrors
       if params[:tour_type][:usual_price_number].blank?
         bad << "Usual price must not be 0.00"
       end
     
  
      rescue ActiveRecord::RecordInvalid
       bad << "#...@tour_type.errors.full_messages}"
    
    end

    unless bad.empty?
     flash.now[:error] = "#{bad.length} Errors have occured in this form"
     flash.now[:items] = bad
     render :update do |page|
        page[:flash].replace_html :partial => 'shared/flash_box' and return
      end
    end
  end

On Friday, February 25, 2011 9:37:05 PM UTC-6, bacrossland wrote:
That is because you did not setup your error handling correctly. A rescue block is done within a begin and end. You don't have that. You have rescues just tossed into your code. The format should look something like this:

begin
  puts 10/0 #Bad code which will throw a ZeroDivisionError
rescue ZeroDivisionError
  puts "Stop dividing by zero or the universe will end!"
end

See the docs on error handling for more information.

http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_exceptions.html

Thanks,
B.

On Fri, Feb 25, 2011 at 9:17 PM, Me <cha...@gmail.com> wrote:
I have this code below, it still gives me the error, ActiveRecord::MultiparameterAssignmentErrors.  It is not catching the rescue for some reason.  Ideas?

def create
    bad =[]
    @tour_type = TourType.new(params[:tour_type])
    if @tour_type.save
     render :update do |page|
       page << "Redbox.close;"
       page.call "ProtoGrowl.success", "Succesfully created #{@tour_type.name}"
     end
    end
    rescue ActiveRecord::MultiparameterAssignmentErrors
     if params[:tour_type][:usual_price_number].blank?
       bad << "Usual price must not be 0.00"
     end
   
    rescue ActiveRecord::RecordInvalid
     bad << "#...@tour_type.errors.full_messages}"
   
    unless bad.empty?
     flash.now[:error] = "#{bad.length} Errors have occured in this form"
     flash.now[:items] = bad
     render :update do |page|
        page[:flash].replace_html :partial => 'shared/flash_box' and return
      end
    end
  end

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to ruby...@googlegroups.com.
To unsubscribe from this group, send email to rubyo...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyo...@googlegroups.com.
To unsubscribe from this group, send email to rubyonra...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment