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’’

Mamadou Touré wrote:

Hi Folks,

I use render :js => “alert(‘Test !’)” in my modele,

In your model? render doesn’t go there.

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 ?

We’d need to know a bit more about the structure of your application and
where this code is to understand. Normally, render statements go in the
controller or the view…

Regards

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Marnen Laibow-Koser wrote:

Marnen Laibow-Koser wrote:

Mamadou Touré wrote:

Hi Folks,

I use render :js => “alert(‘Test !’)” in my modele,

In your model? render doesn’t go there.

OK, just saw your correction.

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 ?

We’d need to know a bit more about the structure of your application and
where this code is to understand. Normally, render statements go in the
controller or the view…

Let’s see your controller code.

Regards

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Hi Marnen, here is my controller code (the method ‘generer’ is triggered
once a user hit a submit button)

def generer
vue = getvue(params[:id])
arr_doc = Array.new()
arr_doc = params[:arr].split(“,”)
params[:arr].gsub!(/[,]/, ‘^’)
vaat_val_lang = vue.langue

if vaat_val_lang.eql?("F")
   vaat_id_lang = 5
else
   vaat_id_lang = 6
end

plsql = nil
plsql = connection.parse('begin 

corr_pck_formul_intellgnt.prc_fi_sous(:p_dect_id
,:p_vaat_id_langue_docu
,:p_list_doc
,
:p_drive); end;')

plsql.bind_param(':p_dect_id', vue.dect_id, Integer)
plsql.bind_param(':p_vaat_id_langue_docu', vaat_id_lang, Integer)
plsql.bind_param(':p_list_doc', params[:arr], String)
plsql.bind_param(':p_drive',path_drive, String)
plsql.exec
render :js => "alert('Test !');"

end

Mamadou Touré wrote:
[…]

Hi Marnen, here is my controller code (the method ‘generer’ is triggered
once a user hit a submit button)

I don’t see offhand why your render statement isn’t working well, but
you’ve got a lot of other problems in your code.

First of all, the entire method may be too long. You may want to split
it up.

def generer
vue = getvue(params[:id])

Why not an ActiveRecord query? And why getvue instead of get_vue ?

arr_doc = Array.new()

That line is unnecessary, since the split in the next line already
creates the array.

arr_doc = params[:arr].split(",")
params[:arr].gsub!(/[,]/, '^')

The brackets in the regex are unnecessary. Actually, the regex itself
can just be a string. Anyway, the whole line is probably unnecessary,
since it doesn’t look like params[:arr] gets referred to after this.

vaat_val_lang = vue.langue

if vaat_val_lang.eql?("F")

Why eql? ? It’s perfectly fine to use == with strings.

   vaat_id_lang = 5
else
   vaat_id_lang = 6
end

You could probably replace this with an AR query.

plsql = nil
plsql = connection.parse('begin 

corr_pck_formul_intellgnt.prc_fi_sous(:p_dect_id
,:p_vaat_id_langue_docu
,:p_list_doc
,
:p_drive); end;')

plsql.bind_param(':p_dect_id', vue.dect_id, Integer)
plsql.bind_param(':p_vaat_id_langue_docu', vaat_id_lang, Integer)
plsql.bind_param(':p_list_doc', params[:arr], String)
plsql.bind_param(':p_drive',path_drive, String)
plsql.exec

Why not just find_by_sql or connection.execute ?

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

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
[email protected]

Marnen Laibow-Koser wrote:

Mamadou Touré wrote:

Hi Folks,

I use render :js => “alert(‘Test !’)” in my modele,

In your model? render doesn’t go there.

OK, just saw your correction.

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 ?

We’d need to know a bit more about the structure of your application and
where this code is to understand. Normally, render statements go in the
controller or the view…

Let’s see your controller code.

Regards

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

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
[email protected]

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 !’);” ?

Mamadou Touré wrote:

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
[email protected]

Thanks a lot for the code review. I’m new in Ruby (coming from an AS/400
background)

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: )

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

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
JavaScript: some MIME types :
application/javascript is only recognized by a few browsers.
text/javascript is the best understood type.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

On 20 July 2010 21:17, Marnen Laibow-Koser [email protected] wrote:


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

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
[email protected]

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 ;))

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

, 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 Im involved in some projects with deadlines, that's why sometimes I copy-paste, but youre 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
JavaScript: some 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 ?
Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Sur M. wrote:

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

Hi Sur,

I did what you suggested, yes it displays the alert, but behind it my
originala screen has disappeared. What I want is to display the alert
without having the screen where the user pressed the submit button
disapears.

regards

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 M. wrote:

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

Hi Sur,

I did what you suggested, yes it displays the alert, but behind it my
originala screen has disappeared. What I want is to display the alert
without having the screen where the user pressed the submit button
disapears.

regards

: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

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:

Sur M. 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 ?

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

2010/7/21 Mamadou Touré [email protected]:

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 ?

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


Hassan S. ------------------------ [email protected]
twitter: @hassan

Sur M. 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() ;-))

You are welcome
I am glad it helped you :slight_smile:

Vous êtes les bienvenus
Je suis content que vous avez aidé :slight_smile:

regards,
Sur
http://crimson9.com

Sur M. 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 ?