How to add javascript directly to the view

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?

On Feb 22, 11:26 am, Adam A. [email protected]
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

On Sun, Feb 22, 2009 at 4:30 AM, Frederick C. <
[email protected]> 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

On Sun, Feb 22, 2009 at 3:26 AM, Adam A.
[email protected] 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. ------------------------ [email protected]

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

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

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