Justin Caldwell wrote:
I’m using “file column” for multiple image uploads. Sometimes it takes
a while for the upload to complete and I would like to provide a tiny
bit of feedback to the user while it’s uploading.
Right now I don’t need a progress bar. I’m using mongrel so don’t even
know it that’s possible yet. Instead, I created a simple RJS template
(my first- woo) that says “File uploading, please wait…” with an
animated gif. Created a simple link_to_remote , tested it out, works
great.
Now, when submitting the form, I can’t figure out how to call both the
RJS template and the action in my controller. Tried render :action =>
‘uploadindicator’ in the action itself but get the “Can’t have multiple
renders” which makes sense.
Played around with ‘submit_remote_tag’, with no luck. :loading looks
like it only works with link_to_remote or form_remote_tag.
Any help would be appreciated.
Woo! a question I can actually answer!
So here’s the deal, in order to see my answer you have to put up with a
brief bout of Turrets syndrome specific to the so-called documentation
in Ruby and Rails:
Ruby and Rails are great but they are completely undocumented!
A list of methods is to documentation as Al Gore is to the internet
So, what I seem to have deduced from my many failures to get similar
things to work is the following:
-
Any, All, and Every (non-private) method in a controller must have
one and only one and not less than one or more than one EXECUTED render
call.
-
The controller method always gets first crack at doing the rendering.
If it chooses not to do any, then Rails will start looking for an
alternative. It looks for a same-named.rhtml file first, and failing
that looks for a same-named.rjs file. Failing all that it complains in
the log that you are a non-rendering idiot.
-
If you are doing ajaxy things, any, all, and every render (with one
exception) must be of the “render :update” variety. Render :update is
called in a block fashion as vaguely described in the “documentation”.
If Rails winds up using an rjs file, it “sticks it in an imaginary
render :update block” for execution.
Given all that, I would recommend giving your rjs file the same name as
the controller method called out in the submit. I would then not do any
rendering at all in your controller method. Rails will see your
reticence to render in the controller method and fall back to looking
for rhtml or rjs files.
An alternative would be to “render :update” your rjs file from the
controller.
hth,
jp
P.S. I’m a rubynuby. Sometimes I get it wrong, in which case my failure
often provokes an expert to chime in. Either way, it all works out for
the best.