Ruby on Rails Friday, February 25, 2011

You're better off handling the validations in the Model. Since these are all ActiveRecord errors you are getting.

If you want to validate in the controller before passing to the model you have to write code that will check for each thing you are looking to validate (is it there?, does it fit the format you want?, etc.) before you pass it to the model. This can be a lot of code and a hassle to maintain. I would highly recommend that you just add a validates_presence_of :usual_price_number in your model and in your controller do something like the following to start:

def create
 @tour_type = TourType.new(params[:tour_type])

 respond_to do |format|
    if @tour_type.save
        flash[:success] = 'Tour was successfully created.'
        format.html {redirect_to(@tour_type)}
        format.xml  { render :xml => @tour_type, :status => :created, :location => @tour_type }
   else
        format.html { render :action => "new" }
        format.xml  { render :xml => @tour_type.errors, :status => :unprocessable_entity }
   end
 end
end

You can customize the error messages for your validates_presence_of and add additional validation types. See Rails documentation for more info.

http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html

B.

On Fri, Feb 25, 2011 at 10:41 PM, Me <chabgood@gmail.com> wrote:
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.

--
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