# CRUD pattern for modals

**URL:** <https://www.ruby-forum.com/t/crud-pattern-for-modals/183268>\
**Category:** Rails\
**Created:** [February 11, 2010, 12:00am UTC](https://www.ruby-forum.com/t/crud-pattern-for-modals/183268 "2010-02-11T00:00:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rorra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df788c/32.png) [@rorra](https://www.ruby-forum.com/u/rorra)\
**Post date:** [February 11, 2010, 12:00am UTC](https://www.ruby-forum.com/t/crud-pattern-for-modals/183268/1 "2010-02-11T00:00:09Z")

</div>

I have an application, where i.e., I can go to the users controllers,  
and they manage all the basic actions by rest, like creating, modifying,  
updating and deleting records.

Now, I want to reuse that code, from another part in the web page, so I  
can list the users on that other controller, and if I press on add, I  
want the users/new page to open, in a popup (like facebox or anything  
like that) but without breaking the code that I already have for  
managing the users (since I want to modify the users for the both  
places).

The main problem is that for the popup, I need another layout to be  
displayed, and once the record is created/updated, I need to display a  
message status instead of redirecting back the user to the users index.

Is there a pattern, a tutorial or something you can point me at so I can  
figure out the best way to do it?

Thanks

---

<div class="post-metadata">

**Author:** ![rorra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df788c/32.png) [@rorra](https://www.ruby-forum.com/u/rorra)\
**Post date:** [February 11, 2010, 5:10am UTC](https://www.ruby-forum.com/t/crud-pattern-for-modals/183268/2 "2010-02-11T05:10:55Z")

</div>

The popup sounds like some AJAX. You can create JavaScript views for  
your actions (action.js.erb) and use XHR with an Accept: text/  
javascript header to get those views (probably users/index.js.erb and  
users/new.js.erb), and do the popup etc. within that. Similar course  
of action for users/create.js.erb, which would display the message  
when it was finished. That’s a quick overview, check out this  
Railscast to get a better idea of how to do that.

> **[\#43 AJAX with RJS - RailsCasts](http://railscasts.com/episodes/43-ajax-with-rjs)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

---

<div class="post-metadata">

**Author:** ![rorra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df788c/32.png) [@rorra](https://www.ruby-forum.com/u/rorra)\
**Post date:** [February 11, 2010, 1:28pm UTC](https://www.ruby-forum.com/t/crud-pattern-for-modals/183268/3 "2010-02-11T13:28:28Z")

</div>

Another option is rb-lightbox, which is made for RoR (and depends on  
prototype)  
[http://code.google.com/p/rb-lightbox-plugin/](http://code.google.com/p/rb-lightbox-plugin/)

It gives you functions like “link\_to\_lightbox” and  
“link\_to\_remote\_lightbox” (ajax).

---

<div class="post-metadata">

**Author:** ![rorra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df788c/32.png) [@rorra](https://www.ruby-forum.com/u/rorra)\
**Post date:** [February 11, 2010, 1:15pm UTC](https://www.ruby-forum.com/t/crud-pattern-for-modals/183268/4 "2010-02-11T13:15:33Z")

</div>

If you really want to have a pop-up and don’t want to deal with Ajax  
at all, then I would suggest fancybox [http://fancybox.net](http://fancybox.net)  
It is actually an iframe, but gives you a popup like feeling and you  
can use your normal controller, just that in click of a link, it will  
open the fancybox and serve you in the iframe.

-Gourav
