# Best practice for showing errors AND updating list

**URL:** <https://www.ruby-forum.com/t/best-practice-for-showing-errors-and-updating-list/69465>\
**Category:** Rails\
**Created:** [February 17, 2006, 1:52am UTC](https://www.ruby-forum.com/t/best-practice-for-showing-errors-and-updating-list/69465 "2006-02-17T01:52:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [February 17, 2006, 1:52am UTC](https://www.ruby-forum.com/t/best-practice-for-showing-errors-and-updating-list/69465/1 "2006-02-17T01:52:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [February 17, 2006, 5:01am UTC](https://www.ruby-forum.com/t/best-practice-for-showing-errors-and-updating-list/69465/2 "2006-02-17T05:01:33Z")

</div>

Michael B. 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](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](http://api.rubyonrails.com/classes/ActionController/Base.html#M000178)

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [February 17, 2006, 8:47am UTC](https://www.ruby-forum.com/t/best-practice-for-showing-errors-and-updating-list/69465/3 "2006-02-17T08:47:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [February 17, 2006, 11:11am UTC](https://www.ruby-forum.com/t/best-practice-for-showing-errors-and-updating-list/69465/4 "2006-02-17T11:11:10Z")

</div>

Thanks guys 🙂 that told me exactly what I was after

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [February 17, 2006, 3:52pm UTC](https://www.ruby-forum.com/t/best-practice-for-showing-errors-and-updating-list/69465/5 "2006-02-17T15:52:51Z")

</div>

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.
