开发者

Learning rails and AJAX. How do I load a div via ajax and then toggle hiding/showing it?

开发者 https://www.devze.com 2022-12-28 12:38 出处:网络
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 g

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 messag开发者_StackOverflowe. 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.


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');" %>
0

精彩评论

暂无评论...
验证码 换一张
取 消