render :js => "alert('Test !')"

Hi Folks,

I use render :js => "alert('Test !')" in my modele, but instead of
having the alert displayed, the browser try to donload a file containing
this string : <<render :js => "alert('Test !')">>. Do you know why I the
browser behaves such a way ?

Regards

Sorry, in the text before I made a mistake, please read ''in my
controller'' instead of ''my model''

This should work. In fact, it sounds like it *is* working -- it's
sending the text to the browser just like it should. What it may not be
doing properly is setting the MIME type. Can you check the MIME type of
the response?

  end

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org

Thanks a lot for the code review. I'm new in Ruby (coming from an AS/400
background), that's why I just ''copy-paste'' code from other ruby
application.

The Mime-type is text/javascript, and I think this one is obsolete , and
I want to set my response Mime-type to 'application/javascript'', how
to do that ? should I add it to render :js => "alert('Test !');" ?

Wow, so was mine (RPG that is, in my case on an IBM360 I think). It
was about 1968. Though looking on Wikipedia I see that it is not much
like Report Program Generator any more. And I am not at all nostalgic
about it, it was absolute rubbish.

Colin

AS/400 isn't a language...

(FWIW, my first professional programming job was working in RPG on an
AS/400. I'm better now, thanks. :slight_smile: )

--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org

Yeah of course I know AS/400 is not a language (the language I used on
AS/400 were RPG/400 and the CLP), AS/400 is just a system like the
IBM36, IBM38, IBM34 etc... My first job also was on AS/400 environnement
in 1994 ;))

, that's why I just ''copy-paste'' code from other ruby
application.

Ooooh. That's really bad. You need to learn the basics of the language
a bit better, I think.

Yeah I started 4 months ago to learn Ruby (using books such Ruby on
Rails bible, Railspace). Right now I`m involved in some projects with
deadlines, that's why sometimes I copy-paste, but you`re right I will
avoid that

The Mime-type is text/javascript, and I think this one is obsolete , and
I want to set my response Mime-type to 'application/javascript'', how
to do that ? should I add it to render :js => "alert('Test !');" ?

You don't want to do that. Check out
http://krijnhoetmer.nl/stuff/javascript/mime-types/ :
application/javascript is only recognized by a few browsers.
text/javascript is the best understood type.

So In my response the mime-type is 'text/javascript', but the isssue is
still here..., so what's wrong then ?

render :js is something that can be used in place of the render :update.
Which means, the request type should be XHR and not HTTP... in simple
words, the render :js will only work for Ajax requests.

If you want something of this sort for the normal HTTP requests... it
can be achieved by something like this in your controller...

@js = "alert('hi')" # or whatever set of js statements needed
render :inline => "<%= javascript_tag(@js) %>"

regards,
Sur

http://crimson9.com

You need to use Ajax there.

Instead of form_for you will need to use form_remote_for.

regards,
Sur

http://crimson9.com

Mamadou Touré wrote:

Sur Max wrote:

You need to use Ajax there.

Instead of form_for you will need to use form_remote_for.

regards,
Sur

http://crimson9.com

Thanks Sur, it works, but I need one more thing. When I hit the submit
button, this will trigger process that creates the xdp files, but with
this change now, the page is too "silent" during the process (the mouse
cursor does not move, everything is quite), so the user would thing that
nothing is happening. Is there a way to have the cursor mouse like busy
during the process ?

:slight_smile:

I would suggest you to read about the Ajax/RJS in general and in Rails.
There are numerous free books/resources available.

For the stuff you just asked, it's like this....

It's called indicator, for which most of the world use an animated gif
image for the purpose.
So you need to put an indicator image in a hidden state. And then you
can show/hide it based on the ajax request status.
Here you can pick up any gif image you would like
http://mentalized.net/activity-indicators/
there are numerous more available on web.

And can implement it as following....

Say, your image is indicator.gif
put your gif image anywhere in your form or on your page like this...

<%= image_tag "indicator.gif", :id => "any_uniq_indicator_id", :style =>
"display:none" %>

And add this to your form_remote_tag code... (ps: the loading and
complete parameters)

<%= form_remote_for :some_symbol, :url => the_url_youve_given, :loading
=> "$('any_uniq_indicator_id').show()", :complete =>
"$('any_uniq_indicator_id').show()" %>

regards,
Sur

http://crimson9.com

Mamadou Touré wrote:

Correct it....

<%= form_remote_for :some_symbol, :url => the_url_youve_given, :loading
=> "$('any_uniq_indicator_id').show()", :complete =>
"$('any_uniq_indicator_id').hide()" %>

In complete, it should be hide()

regards,
Sur
http://crimson9.com

http://www.w3.org/TR/CSS21/ui.html#propdef-cursor

Sur Max wrote:

Correct it....

<%= form_remote_for :some_symbol, :url => the_url_youve_given, :loading
=> "$('any_uniq_indicator_id').show()", :complete =>
"$('any_uniq_indicator_id').hide()" %>

In complete, it should be hide()

regards,
Sur
http://crimson9.com

Thanks a lot Sur, it works perfect ! "Ça marche comme sur des
roulettes" as we say in french. yeah for the little mistake, I guessed
it would be .hide() ;-))