how to add javascript directly to the view

Adam Akhtar 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 <div id='no-items-msg'> 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.

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