I'm wrestling with trying to get some Ajaxy UI going in a Rails 2.3.5
app using jQuery's colorbox pop-up/modal plugin. I don't currently
have the time to port it to Rails 3 just in case anyone wonders why
I'm still using 2.3 (it's a big app).
I have a link on a form that opens up a colorbox window in which the
user can add a new a new model instance that is then selected in a
select on the main form. Basically, if the option they want isn't
shown in the select they can use this pop-up to create and select it.
Opening the form in colorbox is no problem, the issue is in how to
handle the form submission and possible validation errors inside of
the colorbox modal display.
If all goes well and the new model validates then I have a
create.js.erb that updates the select on the main form and sets its
value to the new model instance that was created in the colorbox pop-
up. All is well here.
If there's a validation error, though, I want to effectively just re-
render the "new" form with the bog-standard Rails model validation
errors. And this is the part that's driving me a bit insane.
In order to render the :new template into the colorbox display area I
created a "create_error.js.erb" template that looks like this:
jQuery.colorbox({html: "<%= escape_javascript(render :template
=> :new) %>"});
But this didn't work. Nothing happened and all I saw was a ROLLBACK
in the dev log due to the (intentional) model validation error.
I opened Firebug and looked at the Net > XHR panel. There I found the
response received from the call was:
jQuery.colorbox({html: " "});
The render call was completely swallowed up. Can anyone explain to me
what's happening here?
No matter what I tried it wouldn't render the template. I've used
this technique before with partial templates so I made a copy of the
new view and named it _new, then did this:
jQuery.colorbox({html: "<%= escape_javascript(render :partial =>
'locations/new') %>"});
And now it worked -- mostly. The rendered partial didn't have the
correct formatting, which I tracked down to it not using the correct
layout. The layout for the controller was a pared-down one just for
this case called "popup". Why didn't it use the correct layout?
So I decided to be explicit:
jQuery.colorbox({html: "<%= escape_javascript(render(:partial =>
'locations/new', :partial => 'layouts/popup) %>"});
And now it failed complaining that it couldn't find the layout named
"_popup". This is a Rails 2.3.5 app, and I thought partial layouts
weren't added until later. But to play along I created a new layout
called _popup and finally, it all worked.
I'm scratching my head on how I should be doing this. I simply want
to render the content of a view template into the colorbox area.
Having to "partialize" the view and the layout seems totally goofy to
me.
Can anyone suggest a better, saner, DRYer approach?
Cheers,
Chris
--
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby