Forum: Ruby on Rails Best practice for showing errors AND updating list

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Michael B. (Guest)
on 2006-02-17 02:52
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

- Michael
Mark Reginald J. (Guest)
on 2006-02-17 06:01
(Received via mailing list)
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:

Set the reponse status in your controller using the :status parameter
to render:

We develop, watch us RoR, in numbers too big to ignore.
Jonathan V. (Guest)
on 2006-02-17 09:47
Using RJS (requires rails trunk) is probably the simplest way, you can
then control which parts of the page are updated. Your action may

  def add_user
    @user = User.create(params[:user])

And add_user.rjs:

  if @user.valid?
    page.insert_html :top, 'user_list', :partial => 'user', :object =>
    page.replace_html 'errors', :partial => 'errors', :object =>

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....

Whatever takes your fancy :)

Cheers, -Jonny.
Michael B. (Guest)
on 2006-02-17 12:11
Thanks guys :) that told me exactly what I was after
Nick S. (Guest)
on 2006-02-17 16:52
(Received via mailing 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.
This topic is locked and can not be replied to.