Victor Rosillo
2006-Jul-24 16:58 UTC
[Rails] Ajax page.replace_html model validation render partial errors
Hi; I have the following setup: A model: that validates using helpers like: validates_presence_of, etc.. A view: called ''new'' which renders a partial The partial: _form which has error_messages_for and a form_remote_tag , submit_tag and end_form_tag An action ''create'': that gets executed via the submit_tag in the _form and which in case it can''t save, it renders a rjs template, that replaces the html in the view with the _form so as to present it to the user again along with the associated error messages from the failed validation. The rjs template called ''fixer'' that does: page.replace_htm The problem: If the action ''create'' can''t save because of an error on the validation process, say validates_presence detects an empty field, then the ''create'' action will render the ''fixer'' rjs which has a page.replace_html that replaces the old _form in the view with a new one along with the error messages from the failed validation. So if that happens, I am getting in the view javascript trash and the _form rendered twice. I don''t know why. Here is the code: I have a view called ''new'' that renders a partial definded like this: <div id="info2"> <%= render (:partial => ''form'') %> </div> the partiall _form has this: <%= error_messages_for ''fotografoinfo'' %> <%= form_remote_tag(:update => "info2", :url => {:controller => ''admin'', :action => ''create''}) %> <p><label for="fotografoinfo_nombre">Nombre</label><br/> <%= text_field ''fotografoinfo'', ''nombre'' %></p> <%= submit_tag "Register" %> <%= end_form_tag %> the ''create'' action in the controller looks like this: def create @fotoinfo = Fotografoinfo.new(params[:fotografoinfo]) if @fotoinfo.save flash[:notice] = ''Fotoinfo was successfully created.'' redirect_to :action => ''list'' else render :action => ''fixer'' end end Then I have a rjs called fixer.rjs with this: page.replace_html ''info2'', :partial => ''form'' Well it is not working, I get the partiall rendered two times into the view ''new'' and there is a lot of javascript text and and errors that say: try { Element.update("datos_info2", " 1 error prohibited this fotografoinfo from being saved There were problems with the following fields: * Nombre : Llena la informacion \n \n \n\n\n Nombre \n \n\n \n\n\n\n \n \n\n\n"); } catch (e) { alert(''RJS error:\n\n'' + e.toString()); alert (''Element.update(\"info2\", \" 1 error prohibited this fotografofacturainfo from being saved There were problems with the following fields: * Nombre : Llena la informacion \n \n \n\n\n Nombre \n \n\n \n \n\n\n\n \n \n\n\n\");''); throw e } Thanks a lot for your help