Serodis
Serodis

Reputation: 2112

Ruby on Rails - Suppress an error message

I am using Rails 3 and AJAX and have a parent object which is being created through and AJAX request. This parent is sent with children and saves them all. However, if a child has an error, Rails will stop the request. Is there any way to tell Rails to ignore this? I understand the proper thing to do is find the problem within the Javascript sending the request and fix it. However, for the sake of learning, is there a way to tell Rails that some errors might be ignorable?

Upvotes: 0

Views: 1271

Answers (2)

brettish
brettish

Reputation: 2648

To save without validating use:

@parent.save(:validate => false)

Also, don't forget you can create conditional validation rules if needs be. For example, add a virtual attribute (an instance variable that is not persisted to the DB) accessible via bare_bones?. Then modify a validator like so:

validates_presence_of :nickname, :unless => "bare_bones?"

Then, in your controller you would do something like this:

@parent = Parent.new params[:parent]
@parent.bare_bones = true
@parent.save()

Hope this helps.

Upvotes: 1

Fareesh Vijayarangam
Fareesh Vijayarangam

Reputation: 5052

You are looking for exception handling.

begin
  #code that may contain errors
rescue
  #what to do if an error is encountered
end

Upvotes: 1

Related Questions