Render RJS and action from submit_tag?

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.

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:

  1. 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.

  2. 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.

  3. 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.

Thanks for the info. I think I’m getting closer.

However, I really do want to render two things. When the files are
uploading (beginning of method), I want to display a message
(Uploading…) and when the files complete (method completes) I want to
display another message (Images successfully uploaded.).

I can’t call the rjs directly because I’m uploading files with
“:multipart => true” and ajax can’t handle files/images.

Played around with a hidden iframe, but can’t get around having to
render twice.

So I’m still stumped.

BTW, I had a misprint in my original post. I said that I’ve tried the
“render :action =>” and meant to say “render :update =>”.

On 08 Sep 2006, at 22:54, Justin Caldwell wrote:

“:multipart => true” and ajax can’t handle files/images.

Played around with a hidden iframe, but can’t get around having to
render twice.

So I’m still stumped.

Have a look at the plugin here: http://sean.treadway.info/svn/
plugins/responds_to_parent/README
It lets your use respond_to_parent and an rjs block to get your rjs
to eval in the parent window. This uses the hidden iframe solution
and it works well from what i’ve heard.

Best regards

Peter De Berdt