6

I have a number of standard rails validations within my model:

validates_presence_of :url_string
validates_uniqueness_of :url_string
validates_presence_of :stream_source
validates_presence_of :width
validates_presence_of :height
validates_presence_of :name
validates_uniqueness_of :name
validates_presence_of :customer_name
validates_presence_of :iframe_background_color

If I don't fill out one of these fields within my form then I am taken back to the form as expected but the odd thing is no error messages are displayed. I am using the code below to display the error messages:

<% @camera.errors.full_messages.each do |error| %>
  <p><%= error %></p>
<% end %

I also attempted to print out the @camera.errors object and this is what is shown:

#<ActiveModel::Errors:0x12db19bc @base=#<Camera id: 1, stream_source: "test", width: 640, height: 360, active: true, name: "test", url_string: "CAYD19Vp", customer_name: "test", iframe_background_color: "#FFFFFF", online: true, created_at: "2011-08-30 15:54:16", updated_at: "2011-09-06 15:52:48", audio: true, iframe_text_color: "#FF00FF", iframe_link_color: "#FF0000", notes: "Some notes!", offline_image_file_name: "Cake.jpg", offline_image_content_type: "image/jpeg", offline_image_file_size: 196591, offline_image_updated_at: "2011-09-06 12:12:38", pull_stream_url: "test", bitrate: "300-500", show_branding: false>, @messages={}>
#

As you can see the messages hash is empty. I tried setting the validation error message manually by doing the following:

validates_presence_of :name, :message => "No name present" 

but it did not populate the messages hash either.

Controller update action is shown below:

def update
  @camera = Camera.find(params[:id])
  if @camera.update_attributes(params[:camera])
    flash[:notice] = "Camera updated"
    redirect_to nwcadmin_camera_path
  else
    redirect_to :action => :edit
  end 
end 

I am using Ruby version ruby 1.9.2p290 and Rails version 3.1.0.

Any assistance would be great!

Thanks

4
  • 1
    The controller code will probably be helpful here. Commented Sep 6, 2011 at 16:20
  • Thanks, I have added the controller update action. Commented Sep 6, 2011 at 16:27
  • I managed to get to the bottom of my problem but as I can't answer my own question for another 7 hours I will put the solution here for the time being. In the controller I was using: redirect_to :action => :edit I should have been using: render :action => :edit By using redirect_to I was hitting the edit action within the controller which was then getting a new camera object from the database rather than preserving the camera object from the update action. Commented Sep 6, 2011 at 16:53
  • Possible duplicate of Rails getting validation failed error, but no errors in ActiveRecord error model Commented Jun 28, 2016 at 10:37

5 Answers 5

6

Just a heads up that you'll get a Validation failed (ActiveRecord::RecordInvalid) error with an empty error message (if there are no other errors) when you have before_validation declarations and any of them returns false.

Note that before_validation callbacks must not return false (nil is okay) and this can happen by accident, e.g., if you are assigning false to a boolean attribute in the last line inside that callback method. Explicitly write return true in your callback methods to make this work (or just true at the end if your callback is a block (as noted here)).

UPDATE: This will no longer be an issue starting Rails 5.0, as return false will no longer halt the callback chain (throw :abort will now halt the callback chain).

UPDATE: You might also receive ActiveRecord::RecordNotSaved: Failed to save the record if a callback returns false.

Sign up to request clarification or add additional context in comments.

2 Comments

wow. i never would have guessed this. thanks for saving me probably hours of inspecting!
@LawrenceWu, yeah, spent hours to find the issue myself. :-)
5

I managed to get to the bottom of my problem. In the controller I was using:

redirect_to :action => :edit

I should have been using:

render :action => :edit

By using redirect_to I was hitting the edit action within the controller which was then getting a new camera object from the database rather than preserving the current camera object from the update action.

Comments

2

Unless you call @camera.save or @camera.valid?, the errors hash will not be populated with the validation errors. Please check your controller code.

1 Comment

update_attributes does run validations as it runs the save method after it has updated it's attributes. You can see this if you view the source of update_attribute. apidock.com/rails/ActiveRecord/Base/update_attributes
2

You can use flash[:message] or flash[:notice] in controller code to store the error message, which can be used in view to display the errors.Link Have a look in the link,it's clearly explained, how to append the error messages and use them to display.The instance variable doestnot contains any errors as no validation runs in update.

You can use @camera_errors = @camera.save to collect the errors and then

  <% @camera_errors.errors.full_messages.each do |error| %>
   <p><%= error %></p>
  <% end %>

Comments

1

I'm not sure if this is something that you might be interested in or not, but you can use this official Rails gem: dynamic_form

This gem provides you two helper methods: error_messages and error_messages_for

Refer to the following Rails guide for more: http://guides.rubyonrails.org/active_record_validations_callbacks.html#displaying-validation-errors-in-the-view

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.