Michael Bryne
2006-Feb-17 00:52 UTC
[Rails] Best practice for showing errors AND updating list
Morning/Afternoon/Evening All, This is my first post to the ruby message board so if my request is better suited to another location just let me know, The problem I keep coming up against is with a listing of objects and updating that list via AJAX (for arguments we are talking about a list of users). As it stands I can currently submit a form, and update the list (using :update => ''list_div_name'' and generating the whole list again or generating a partial of a single row and using Insertion.Top) and I can also display the errors for a form using the same method and updating a notification div above my form, However, my problem occurs when I want to update a list on success and display errors on failure, I have figured the best way up until now would be to always update the forms notification and either display the errors or generate a hidden div and copy its contents to the top of the list if successful but this seems far from efficient, What I am essentially asking is there a standard / more efficient way of doing this that I have missed completely or is my current method suitable? - Michael -- Posted via http://www.ruby-forum.com/.
Mark Reginald James
2006-Feb-17 03:58 UTC
[Rails] Re: Best practice for showing errors AND updating list
Michael Bryne wrote:> However, my problem occurs when I want to update a list on success > and display errors on failure,Look at the :success and :failure parameters of link_to_remote, which can also be inside the :update parameter. Also available in submit_to_remote, form_remote_tag, periodically_call_remote, remote_function, observe_field, and observe_form: http://api.rubyonrails.com/classes/ActionView/Helpers/JavaScriptHelper.html#M000433 Set the reponse status in your controller using the :status parameter to render: http://api.rubyonrails.com/classes/ActionController/Base.html#M000178 -- We develop, watch us RoR, in numbers too big to ignore.
Jonathan Viney
2006-Feb-17 07:47 UTC
[Rails] Re: Best practice for showing errors AND updating list
Using RJS (requires rails trunk) is probably the simplest way, you can then control which parts of the page are updated. Your action may become... def add_user @user = User.create(params[:user]) end And add_user.rjs: if @user.valid? page.insert_html :top, ''user_list'', :partial => ''user'', :object => @user else page.replace_html ''errors'', :partial => ''errors'', :object => @user.errors end Make sure you remove the :update => bit from your helper call as well. You can actually now put the RJS in the controller if you like... def add_user ... render :update do |page| if @user.... page.replace_html end end Whatever takes your fancy :) Cheers, -Jonny. -- Posted via http://www.ruby-forum.com/.
Michael Bryne
2006-Feb-17 10:11 UTC
[Rails] Re: Best practice for showing errors AND updating list
Thanks guys :) that told me exactly what I was after -- Posted via http://www.ruby-forum.com/.
Nick Stuart
2006-Feb-17 14:50 UTC
[Rails] Re: Best practice for showing errors AND updating list
Quick note, there is actually a plugin to allow you to use RJS outside of the Trunk release. Can''t remember the site, but do a quick google for it and you''ll run across it. Works great for me. On 2/17/06, Jonathan Viney <jviney@spreydon.org.nz> wrote:> Using RJS (requires rails trunk) is probably the simplest way, you can > then control which parts of the page are updated. Your action may > become... >