Ruby Forum Ruby on Rails > ActiveRecord validation with ajax calls

Posted by skg (Guest)
on 07.05.2008 14:04
(Received via mailing list)
Hi
  I have a model class ServiceDeskResolution.It has fields, code and
solution.I have to make the solution not empty .So for that I wrote
validates_presence_of :resolution
 in the model.So from the view when I click save nothing is saved to
database.This is what I need.But no error messages are printed and the
view goes to next window..How can I print error messages in the
current window?

View code (define_sd_resolution.rhtml)
<%= error_messages_for 'service_desk_resolution' %>   #This not
working
-------
-------code

<%= submit_to_remote 'save_btn', 'Save',
      :html => {:class => "itilbutton1"},
     :url => {:controller => 'service_desk',  :action =>
'sd_resolution_save', :id => @sd_ticket.id, :sd_resolution_id =>
@sd_resolution_id}, :before =>
"checkResolution(document.getElementById('sd_resolution_service_desk_resolution_code_id'),
'Please Choose Resolution');"  %>

controller code
def sd_resolution_save

@service_desk_resolution=ServiceDeskResolution.new(params[:sd_resolution])
        @service_desk_resolution.save
        render :action => "sd_resolution_save.rjs"

end


what I need is if !@service_desk_resolution.save    how can i revert
this back to the same define_sd_resolution.rhtml and print the
validation message on that.
Thanks in advance
Sijo
Posted by Rails Terrorist (malioboro)
on 07.05.2008 16:17
I have good suggestion if you spend 5 minutes to 
http://livevalidation.com/examples, and download their script :D. I hope 
it will help you.

I am not sure your code here, maybe it should be :

def sd_resolution_save

@service_desk_resolution=ServiceDeskResolution.new(params[:sd_resolution])
  if @service_desk_resolution.save
     render :action => "sd_resolution_save.rjs"
  else
     render :action => 'define_sd_resolution'
     #render :file => "#{RAILS_ROOT}/public/define_sd_resolution.rhtml"
     # I am not sure with render file.
  end
end

your code before dont have block to break error, so it will execute 
:action => "sd_resolution_save.rjs" although it is error or failed 
validation.


Good Luck,
http://Teapoci.Blogspot.com
Reinhart