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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9f39e8fe55b0e29a36bcbf43c588ea3d?d=identicon&s=25 Michael Bryne (mbryne)
on 2006-02-17 01: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
suitable?

- Michael
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-02-17 05:01
(Received via mailing 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/Help...

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

http://api.rubyonrails.com/classes/ActionControlle...

--
We develop, watch us RoR, in numbers too big to ignore.
337cdd270761e0e6f4356de45b04d388?d=identicon&s=25 Jonathan Viney (jonny)
on 2006-02-17 08: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
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.
9f39e8fe55b0e29a36bcbf43c588ea3d?d=identicon&s=25 Michael Bryne (mbryne)
on 2006-02-17 11:11
Thanks guys :) that told me exactly what I was after
Cb610750ee94ca103aef4b2dc7b1b768?d=identicon&s=25 Nick Stuart (Guest)
on 2006-02-17 15: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.