opensoul.org

Using RedBox for modal dialogs

When I first saw Lightbox, my first thought (ok, second thought; my first was “This is awesome!”) was: “This would be a great technique for confirmation pages!” I never got around to using lightbox due to it’s emphasis on images, but eventually other solutions started showing up, like GreyBox and ThickBox, that allowed you to use other content.

A couple weeks ago, I came across Redbox, a rails plugin based on ThickBox. It is a great little plugin that simply consists of a few Rails helpers and some Javascript.

As I experimented with it, I decided that I really like the feel of using modal dialog boxes for confirmation messages. It is more consistent with the desktop experience, but not as constrained as popups on the desktop.

Redbox Delete

As I was using Redbox, I developed a pattern that works really well. Instead of limiting :destroy requests to POST, I modified them to return a delete confirmation page when called with a GET, then perform the modification when called with a POST.

def destroy
  @payment = Payment.find(params[:id])
  if request.get?
    respond_to do |wants|
      wants.html
      wants.js { render :layout => 'modal' }
    end
  else
    @payment.destroy
    flash[:notice] = "Payment for $#{@payment.amount} has been deleted."
    redirect_to :action => 'index'
  end
end

This creates a little extra code, but it also makes the modal windows degradable. If the client doesn’t support JavaScript, or it is disabled, then when they click a destroy link, they will be taken to a separate confirmation page. I’ve also created a ‘modal’ layout that can have some formating, or maybe a close link.

The code to create the redbox link looks like:

<%= link_to_remote_redbox 'delete',
    {:url => {:action => 'destroy', :id => payment}, :method => :get},
    :href => url_for(:action => 'destroy', :id => payment) %>

link_to_remote_redbox takes the same parameters as link_to_remote, minus the :update parameter that tells where to put the new content. Note the :method =&gt; :get. By default, Ajax requests use POST, but I don’t want to POST to the destroy action because that will perform the modification. I’m also setting :href, which makes this link degradable.

As I used Redbox more and more for confirmation pages, it occurred to me that this method would also work really well for simple forms. So, with barely any modification—adding respond_to in the action and replacing link_to with link_to_remote_redbox—I was able to change these small forms into modal dialogs. Currently, the forms submit a regular request, but I could refactor it a little more and use Ajax to submit the forms and update the page.

Redbox Enroll

Overall, I really like the feel of these modal dialogs. They make the app feel snappier and less daunting. Users don’t have to jump all over the app for simple modifications. They spend the majority of their time on one page, the most important page.

ajax, development, rails, and redbox September 21, 2006

11 Comments

  1. ruby licious ruby licious September 22, 2006

    Good post.. I’ve missed this one up until now, have to incorp. this into my various webadmis.

  2. […] Looks like we keep getting closer to bridging the thin/thick client gap. […]

  3. […] opensoul.org » Using RedBox for modal dialogs […]

  4. Adam Adam February 28, 2007

    Thanks for the post. Some nice ideas. I have been tinkering with redbox but I cant seem to get forms working. Did you have to do something special to allow forms in a redbox.

    I have read all comments to the redbox site and forms seem to be a challenge to others as well.

    Thanks

  5. Adam Adam February 28, 2007

    I figured out a solution using link_to_remote.

  6. John John June 27, 2007

    I am having problems in form posting. I am successful in opening a modal window (nice demo Brandon) using link_to_remote_redbox but not able to do a ajax form posting. Doesn’t form_remote_tag work in modal window forms?

  7. Brandon Brandon June 27, 2007

    John,

    What are you doing in the action that the form submits to? If you send a redirect the Ajax request will follow it.

  8. John John June 27, 2007

    Actually theres a inbox kind-a page where i ve records from the db listed in a table. Each row has got a link_to_remote_Redbox link to open a save contact modal window. So this modal window is a form where I key in the contact’s name and then hit the submit button in the modal window. After htting the submit button the modal should disappear and the original window should be refreshed to flash the new contact’s name in the table. The form posting should be an ajax one so that it only updates the main table’s div area.

  9. Brandon Brandon June 27, 2007

    John,

    The all you need to do is use respond_to in your action that the form submits to and have an RJS page that does whatever updating you want done:

    # controller
    def update
      # do something here
      respond_to do |wants|
        wants.html { redirect_to some_path }
        wants.js
      end
    end
    
    # update.rjs
    page &lt;&lt; "RedBox.close();"
    page.update :my_div, :partial =&gt; "thing", :object =&gt; @thing
    

    You’d probably be better off calling RedBox.close(); on the client side when the form submits.

  10. gilgam gilgam October 23, 2007

    Hi

    just fell upon your blog and as you were, it’s a pleasure to use Redbox.

    I juste have a problem to close the dialog box as the user submits the small form.

    do you have any idea ?

    thanks

    Gilgam

  11. gilgam gilgam October 23, 2007

    oups

    i found it

    just add :onclick => ‘RedBox.close()’ within the submit_tag helper …

Post a Comment

Comments use textile. Anonymous comments will be deleted.

My name is Brandon Keepers. I like to build things, usually in Ruby or JavaScript. I work at GitHub and live in Holland, MI.

Popular Posts