# How to add javascript directly to the view

**URL:** <https://www.ruby-forum.com/t/how-to-add-javascript-directly-to-the-view/161018>\
**Category:** Rails\
**Created:** [February 22, 2009, 12:26pm UTC](https://www.ruby-forum.com/t/how-to-add-javascript-directly-to-the-view/161018 "2009-02-22T12:26:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [February 22, 2009, 12:26pm UTC](https://www.ruby-forum.com/t/how-to-add-javascript-directly-to-the-view/161018/1 "2009-02-22T12:26:25Z")

</div>

Im incorporating ajax / javascript stuff for the first time in my  
project and im a bit confused by all various methods available, rjs,  
prototype, javascript etc so please excuse me if this is an obvious  
question.

Whilst looping though an array of model objects in my view I want to be  
able to show or hide a div that contains the message “Your list is  
empty”.

so something like this

\<% if items.empty?\>  
#the following is not the correct way  
$(‘no-items-msg’).show  
\<% else \>  
$(‘no-items-msg’).hide  
\<% items.each do |item|%\>  
render :partial =\> “item\_rows”…  
\<%end%\>

how do i add the prototype stuff correctly?

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [February 22, 2009, 1:31pm UTC](https://www.ruby-forum.com/t/how-to-add-javascript-directly-to-the-view/161018/2 "2009-02-22T13:31:24Z")

</div>

On Feb 22, 11:26 am, Adam A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> \<% if items.empty?\>  
> #the following is not the correct way  
> $(‘no-items-msg’).show  
> \<% else \>  
> $(‘no-items-msg’).hide  
> \<% items.each do |item|%\>  
> render :partial =\> “item\_rows”…  
> \<%end%\>

First off in javascript the () for function calls is mandatory - $(‘no-  
items-msg’).show just evaluates to a function object, you need $(‘no-  
items-msg’).show() to actually call it. secondly your javascript needs  
to be inside tags (there’s a rails helper that will assist  
with this, can’t remember what it’s called but it has javascript in  
the name.

Fred

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [February 22, 2009, 1:48pm UTC](https://www.ruby-forum.com/t/how-to-add-javascript-directly-to-the-view/161018/3 "2009-02-22T13:48:25Z")

</div>

On Sun, Feb 22, 2009 at 4:30 AM, Frederick C. \<  
removed\_email\_address@domain.invalid\> wrote:

> > Whilst looping though an array of model objects in my view I want to be  
> > \<% items.each do |item|%\>  
> > render :partial =\> “item\_rows”…  
> > \<%end%\>
> 
> First off in javascript the () for function calls is mandatory - $(‘no-  
> items-msg’).show just evaluates to a function object, you need $(‘no-  
> items-msg’).show() to actually call it. secondly your javascript needs  
> to be inside tags (there’s a rails helper that will assist  
> with this, can’t remember what it’s called but it has javascript in  
> the name.

Fred, I believe you’re referring to the javascript\_tag Rails helper.

> Fred
> 
> > how do i add the prototype stuff correctly?

Adam, you’ll need to add the following inside your HTML tag:

\<%= javascript\_include\_tag :defaults %\>

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [February 22, 2009, 2:32pm UTC](https://www.ruby-forum.com/t/how-to-add-javascript-directly-to-the-view/161018/4 "2009-02-22T14:32:25Z")

</div>

On Sun, Feb 22, 2009 at 3:26 AM, Adam A.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Whilst looping though an array of model objects in my view I want to be  
> able to show or hide a div that contains the message “Your list is  
> empty”.

> \<% if items.empty?\>  
> #the following is not the correct way  
> $(‘no-items-msg’).show

Why would you do this with JS?? Just put the message here. Then  
users with or without JS enabled (including spiders and web service  
clients) will get the intended view.

> \<% else \>

## FWIW,

Hassan S. ------------------------ removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [February 22, 2009, 2:52pm UTC](https://www.ruby-forum.com/t/how-to-add-javascript-directly-to-the-view/161018/5 "2009-02-22T14:52:25Z")

</div>

Thank you all for your input. Thank you hassan for answering some of my  
doubts abou this methodology. Basically this is the result of my lack of  
knowledge.

The rows in this table represent tasks in a todolist. Theres a delete  
option for each task via ajax and the deleted row is taken out via  
$().remove\_html calls. when all the rows are taken out though i need to  
show that message. This is true in reverse. When there are initially no  
tasks this message wiill be shown, when a user adds via ajax a task, the  
row is inserted via .insert\_html. Hence the message needs to be hidden.  
I thought it best to do it via a .hide or .show call.

Sorry this is a very quick explanation as my girlfriend is currently  
screaming at me that i spend too much time on the pc and i fear for my  
life.

If anyone has a design pattern \ best practice about how to handle  
tables and messages indicating there are no records plus ajax calls.  
please tell. ill be back tomorow

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [February 23, 2009, 12:48pm UTC](https://www.ruby-forum.com/t/how-to-add-javascript-directly-to-the-view/161018/6 "2009-02-23T12:48:46Z")

</div>

Hi thanks for the reply. What i did in the end was to type the “no  
records” message into the view directly so that non javascript enabled  
users could still see it. I then created some javascript code to handle  
the situations above.

Thanks everyone for yoru help. it really helped me out

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [February 22, 2009, 4:44pm UTC](https://www.ruby-forum.com/t/how-to-add-javascript-directly-to-the-view/161018/7 "2009-02-22T16:44:27Z")

</div>

Adam A. wrote:

> Im incorporating ajax / javascript stuff for the first time in my  
> project and im a bit confused by all various methods available, rjs,  
> prototype, javascript etc so please excuse me if this is an obvious  
> question.
> 
> Whilst looping though an array of model objects in my view I want to be  
> able to show or hide a div that contains the message “Your list is  
> empty”.

Does the program know this when the page renders, or will the message  
change  
during the time your user looks at the page?

> so something like this
> 
> \<% if items.empty?\>  
> #the following is not the correct way  
> $(‘no-items-msg’).show  
> \<% else \>  
> $(‘no-items-msg’).hide  
> \<% items.each do |item|%\>  
> render :partial =\> “item\_rows”…  
> \<%end%\>

Things in \<% erb %\> tags happen only when the page renders, so you could  
just  
put that

 itself inside the \<% if %\> blocks. No  
Javascript.

If the message must change at runtime, then you will need an Ajax call,  
such as  
remote\_function, to call an action on your server. It uses ‘render  
:update’ to  
call show() or hide() on that div.

–  
Phlip
