views:

24

answers:

1

I'm doing my first Ruby on Rails project. I am working on a project where the user can add a new message. Then they can add updates to this message like a thread. I've got it all written and now I'm going back and applying some AJAX. I want to show just the main message when the page first loads. Then when I click a link I want an AJAX event to fire that populates a div with all the updates to the message. Then I want this same link to toggle hiding and showing the div.

I've made a submit button to fire the ajax and get back the updates using form_remote_tag. I've also created a separate link to toggle the hiding using $(div_id).toggle();. But now I'm stuck on how to combine these two things into one all purpose link.

Thanks.

A: 

You could use a Javascript function that behaves differently based on the state of your content.

function content_mgr(content_div) {
   // is $(content_div) empty?
   //     submit form
   // else
   //     $(content_div).toggle();
}

And in your template:

<%= button_to_function "Super Button", "content_mgr('div_id');" %>
jdeseno
I did something very similar to this. Thanks for the advice
Kevin L.