Forum: Ruby on Rails how to add javascript directly to the view

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
803ad415efcdcbf09b1addeb319650d7?d=identicon&s=25 Adam Akhtar (thegeezer3)
on 2009-02-22 12:26
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?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-02-22 13:31
(Received via mailing list)
On Feb 22, 11:26 am, Adam Akhtar <rails-mailing-l...@andreas-s.net>
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 <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
40db9e75b3f5899258e3bdc0c9210154?d=identicon&s=25 Conrad Taylor (Guest)
on 2009-02-22 13:48
(Received via mailing list)
On Sun, Feb 22, 2009 at 4:30 AM, Frederick Cheung <
frederick.cheung@gmail.com> 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 <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, 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 <head> tag:

<%= javascript_include_tag :defaults %>

Good luck,

-Conrad
Bee69cfed999cd13e3bff73d472a39ee?d=identicon&s=25 Hassan Schroeder (Guest)
on 2009-02-22 14:32
(Received via mailing list)
On Sun, Feb 22, 2009 at 3:26 AM, Adam Akhtar
<rails-mailing-list@andreas-s.net> 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 Schroeder ------------------------ hassan.schroeder@gmail.com
803ad415efcdcbf09b1addeb319650d7?d=identicon&s=25 Adam Akhtar (thegeezer3)
on 2009-02-22 14:52
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
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-02-22 16:44
(Received via mailing list)
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.

--
   Phlip
803ad415efcdcbf09b1addeb319650d7?d=identicon&s=25 Adam Akhtar (thegeezer3)
on 2009-02-23 12:48
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
This topic is locked and can not be replied to.