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

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

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

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