Rails do not generate validation error messages

I have some standard checks in 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 do not fill out one of these fields in my form, I will return to the form as expected, but it is strange that error messages do not appear. I use the following code to display error messages:

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

I also tried to print the @ camera.errors object, and here 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 message hash is empty. I tried to set the verification error message manually by following these steps:

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

but he also did not populate the message hash.

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 help would be great!

thanks

+6
source share
5 answers

I managed to figure out my problem. In the controller, I used:

 redirect_to :action => :edit 

I should have used:

 render :action => :edit 

Using redirect_to, I pressed the edit action inside the controller, which then received the new camera object from the database, and did not save the current camera object from the update action.

+5
source

It’s just the head that you will get a Validation failed (ActiveRecord::RecordInvalid) error Validation failed (ActiveRecord::RecordInvalid) with an empty error message (if there are no other errors) when you have before_validation declarations and any of them returns false .

Please note that before_validation callbacks before_validation not return false ( nil is ok), and this can happen by accident, for example, if you assign false boolean attribute in the last line inside this callback method, I will explicitly write return return in the callback methods so that make this work (or just true at the end if your callback is a block (as indicated here )).

UPDATE : this will no longer be an issue when starting Rails 5.0, since return false will no longer stop the callback chain ( throw :abort now stop the callback chain).

UPDATE You can also get ActiveRecord::RecordNotSaved: Failed to save the record if the callback returns false .

+6
source

Should you not call @camera.save or @camera.valid? hash errors will not be populated with validation errors. Check the controller code.

+2
source

You can use flash [: message] or flash [: notice] in the controller code to save an error message that can be used to display errors. Link Look in the link, it clearly explained how to add error messages and use them to display. The doestnot instance variable contains any errors since the update check is not performed.

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

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

I'm not sure if this might interest you or not, but you can use this official Rails stone: dynamic_form

This stone provides you with two helper methods: error_messages and error_messages_for

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

+1
source

Source: https://habr.com/ru/post/896728/


All Articles