# How to use Javascript onclick functionality via Rails helpers

**URL:** <https://www.ruby-forum.com/t/how-to-use-javascript-onclick-functionality-via-rails-helpers/205628>\
**Category:** Rails\
**Created:** [April 11, 2011, 12:05pm UTC](https://www.ruby-forum.com/t/how-to-use-javascript-onclick-functionality-via-rails-helpers/205628 "2011-04-11T12:05:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [April 11, 2011, 12:05pm UTC](https://www.ruby-forum.com/t/how-to-use-javascript-onclick-functionality-via-rails-helpers/205628/1 "2011-04-11T12:05:29Z")

</div>

Hello,

I am trying to implement a functionality similar to the onclick method  
of Javascript, using Rails Helpers.

I have implemented a auto complete feature for more than one field in  
Rails 2.0.2 and Ruby 1.8.7. This configuration is run on Ubuntu 10.04  
and yes , I know… you might feel why Rails 2.x when we have Rails 3.x ,  
it is so because of project specific purposes. I don’t have much of a  
choice on that …🙂 .

I am able to successfully retrieve a group specific profile pic, a group  
title and a the total number of members belonging to a group based on  
the given “title” as the basic search query that a user inputs in the  
text field where auto complete functionality is being performed. Getting  
the auto complete functionality to work for the scenario above was a  
challenge in itself.

In case you aren’t able to get a better understanding of what I have  
implemented wrt autocomplete, kindly refer to

> <https://stackoverflow.com/questions/5399991/implementing-auto-complete-for-more-than-one-field-in-rails>

.

You can also get a better understanding of what I have implemented by  
referring to [nullnetwork.net](http://imagepaste.nullnetwork.net/viewimage.php?id=2048) ,  
[nullnetwork.net](http://imagepaste.nullnetwork.net/viewimage.php?id=2050)  
and [nullnetwork.net](http://imagepaste.nullnetwork.net/viewimage.php?id=2049) .

Please note the name of group specific profile pic is same as that of  
the group title with the exception that the pic would also have .jpg  
appended to it.

I am making use of a customized auto complete method, an auto complete  
plugin and also making use of Rails partials. The cusotmized auto  
complete method is allowing me to modify the basic functionality to suit  
my requirement.

Coming to my question, Now say if a user enters “I” in the text field as  
per the auto complete feature that I have implemented, the results(for  
more than one matches found(i.e. if more than one group exists which  
start with an “I”) on a specific search query) populated on the User  
Interface would have all the following :-

- Group Specific Profile Pic(s) starting  
with name same as title, in this case  
“I%”.jpg
- Group title name.
- Total number of members belonging to  
a group

Say I have India and Independence as my group names I would obtain the  
following results displayed in my auto complete text field.

```
     India.jpg(image will be displayed) India(Group title name

```

displayed) 3(dummy value here)

and similarly

```
     Independence.jpg Independence

```

4

All this would come in one text field. Please note the comments in  
parentheses are not part of the results returned via auto complete.

In case you are not able to get the picture in your mind of how the UI  
would look, Kindly register in [www.diasp.org](http://www.diasp.org) and search for any random  
user using its search feature , once your account is created.

Please observe that the spacing between image, title name and total  
number of group members is too much. I will be referring back to this in  
the comments that follow.

The code for the partial that renders all this is:-

```
<ul>
  <% for inv_group in @investor_group2 %>
  <%=image_tag "investor_groups/#{inv_group.title}.jpg" , :width =>

```

‘40’, :height =\> ‘22’, :align =\> ‘left’ %\>

- \<%=h inv\_group.title  
%\>(\<%=h inv\_group.activated\_members.size %\>)
  
\<%end%\>  

Now if a user hovers his/her mouse over say India and then clicks on it,  
the text field is populated with `**India(3)**` .

What I need is to implement is a **on click in Rails** which will  
**select only Group title**. I tried to implement the group title and  
group size with separate `<li>` tags , I was then able to mouseover and  
select only the title and get it populated for the text field.

The problem with this approach is it completely spoils the formatting  
and the spacings/gaps between the image, the title and the group size.  
This requires formatting in order to obtain a better look and feel of  
the UI.

If I can figure out how to reduce the formatting space between each of  
the three title.jpg,title and group size. This option could be  
considered to be an alternative fix but might not be the best fix.

I am not too sure how can I go about the same(the suggested fix) as I am  
a newbie wrt many things in RoR. I am not too sure on what commands of  
RoR I can make use of which would help me simplify/solve my task. Can  
you please help me on this.

I would be also grateful for any suggestions on a more efficient fix to  
this scenario.

I later on, in a way was able to figure out that by using javascript’s  
onclick event, I could solve my problem. I am not too sure about the  
equivalent syntax for the same using Rails Helpers.I referred to  
[http://api.rubyonrails.org/classes/ActionView/Helpers/JavaScriptHelper.html#method-i-javascript\_tag](http://api.rubyonrails.org/classes/ActionView/Helpers/JavaScriptHelper.html#method-i-javascript_tag)  
, to get a hang of how the javascript\_tag can be used for my purpose,  
but if this is the way out, I am really clueless on how to modify the  
basic syntax to suit my requirements. Kindly help me with the same if  
feasible.

Thank you…

---

<div class="post-metadata">

**Author:** ![mohnish\_j55](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a88e4f/32.png) [@mohnish\_j55](https://www.ruby-forum.com/u/mohnish_j55)\
**Post date:** [April 11, 2011, 3:32pm UTC](https://www.ruby-forum.com/t/how-to-use-javascript-onclick-functionality-via-rails-helpers/205628/2 "2011-04-11T15:32:55Z")

</div>

On Apr 11, 2011, at 6:05 AM, Mohnish G j wrote:

> choice on that ..🙂 .  
> In case you aren’t able to get a better understanding of what I have  
> complete method is allowing me to modify the basic functionality to
> 
> - Group Specific Profile Pic(s) starting  
> displayed) 3(dummy value here)  
> would look, Kindly register in [www.diasp.org](http://www.diasp.org) and search for any random  
> \<% for inv\_group in @investor\_group2 %\>  
> What I need is to implement is a **on click in Rails** which will  
> If I can figure out how to reduce the formatting space between each of  
> to
> 
> Thank you..

[Script.aculo.us](http://Script.aculo.us) Ajax.Autocompleter has a feature for just this  
purpose. You can strip your entire onclick business back out, and use  
just plain vanilla Autocompleter. All you need to do is change a tiny  
thing in your Rails view that returns the autocomplete results.  
Everything that you don’t want to be populated into the result field  
when a user clicks should be wrapped in a span.informal.

- [image tag]IndiaSome other descriptive text.

If a user clicks on that option in the suggestion list, only the word  
India will be entered into the text field that spawned the  
autocompleter.

So in your Rails template for the autocompleter results, just wrap  
everything that you don’t want to be entered in a span.informal, and  
you can have at it. If you wanted fake columns in your results, you  
could do:

- [image tag]IndiaSome other descriptive text.

and use more CSS on .icon and .description to make those “columns” a  
particular width.

Walter

---

<div class="post-metadata">

**Author:** ![mohnish\_j55](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a88e4f/32.png) [@mohnish\_j55](https://www.ruby-forum.com/u/mohnish_j55)\
**Post date:** [April 12, 2011, 7:11am UTC](https://www.ruby-forum.com/t/how-to-use-javascript-onclick-functionality-via-rails-helpers/205628/3 "2011-04-12T07:11:48Z")

</div>

Walter D. wrote in post #992117:

> On Apr 11, 2011, at 6:05 AM, Mohnish G j wrote:
> 
> > choice on that ..🙂 .  
> > In case you aren’t able to get a better understanding of what I have  
> > complete method is allowing me to modify the basic functionality to
> > 
> > - Group Specific Profile Pic(s) starting  
> > displayed) 3(dummy value here)  
> > would look, Kindly register in [www.diasp.org](http://www.diasp.org) and search for any random  
> > \<% for inv\_group in @investor\_group2 %\>  
> > What I need is to implement is a **on click in Rails** which will  
> > If I can figure out how to reduce the formatting space between each of  
> > to
> > 
> > Thank you..
> 
> [Script.aculo.us](http://Script.aculo.us) Ajax.Autocompleter has a feature for just this  
> purpose. You can strip your entire onclick business back out, and use  
> just plain vanilla Autocompleter. All you need to do is change a tiny  
> thing in your Rails view that returns the autocomplete results.  
> Everything that you don’t want to be populated into the result field  
> when a user clicks should be wrapped in a span.informal.
> 
> - [image tag]IndiaSome other descriptive text.
> 
> If a user clicks on that option in the suggestion list, only the word  
> India will be entered into the text field that spawned the  
> autocompleter.
> 
> So in your Rails template for the autocompleter results, just wrap  
> everything that you don’t want to be entered in a span.informal, and  
> you can have at it. If you wanted fake columns in your results, you  
> could do:
> 
> - [image tag]IndiaSome other descriptive text.
> 
> and use more CSS on .icon and .description to make those “columns” a  
> particular width.
> 
> Walter

Hi Walter,

Thanks a lot , your solution of using the span tag did exactly what I  
needed. I have to still play around with trying to fix the width wrt the  
fields using CSS. I shall post queries on this , if any.

I have posted the same question on stackoverflow(  
[http://stackoverflow.com/questions/5595613/implementing-a-onclick-kind-of-functionality-and-formatting-wrt-rails-partial-vie](http://stackoverflow.com/questions/5595613/implementing-a-onclick-kind-of-functionality-and-formatting-wrt-rails-partial-vie)  
).

If you have a profile there, kindly post your answer there so that I  
could accept the same. Or if you wouldn’t want to do it, I with your  
permission would submit the same for it to be useful to anyone in the  
open source community…

Thanks again..

---

<div class="post-metadata">

**Author:** ![mohnish\_j55](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a88e4f/32.png) [@mohnish\_j55](https://www.ruby-forum.com/u/mohnish_j55)\
**Post date:** [April 12, 2011, 3:49pm UTC](https://www.ruby-forum.com/t/how-to-use-javascript-onclick-functionality-via-rails-helpers/205628/4 "2011-04-12T15:49:11Z")

</div>

On Apr 12, 2011, at 2:56 AM, Mohnish J. wrote:

> > > > would look, Kindly register in [www.diasp.org](http://www.diasp.org) and search for any
> > > 
> > > class=“informal”\>Some other descriptive text.
> > 
> > If you have a profile there, kindly post your answer there so that I  
> > html concepts,  
> > \<%=image\_tag “investor\_groups/#{inv\_group.title}.jpg” , :width =\>  
> > .description{  
> > over  
> > to it(Kindly refer:-  
> > [http://imagepaste.nullnetwork.net/viewimage.php?id=2054](http://imagepaste.nullnetwork.net/viewimage.php?id=2054) ). I am not  
> > sure  
> > where I am doing what wrong that the functionality is getting affected  
> > as a result.
> 
> Kindly guide me to correct my mistake(s) on this
> 
> Thanks..

Not sure if you’ve already sorted this out, but if I’m reading this  
correctly, your code will be generating something similar to this cut-  
down example:

ul  
img/  
li  
some text  
/li  
img/  
li  
some text  
/li  
…  
/ul

This is completely invalid HTML, so getting CSS to do anything useful  
with it will be next to impossible.

What you must do is get that image inside the li, where it belongs,  
and if you don’t want it to be in your autocompleter populated  
content, then wrap the image in a span.informal. If this isn’t for  
your autocompleter results, then you still must put the images inside  
the li tags, but you have all sorts of tools to get the layout to work  
the way you expect once you do that.

One of the best tools you can use to get this sort of problem sorted  
is first to go the [validator.w3.org](http://validator.w3.org) and either put in the URL of your  
(public) site, or paste in the View Source rendered code from your  
browser in the Direct Input field. Either one will point out where  
your layout fails to validate. Once you have valid HTML (think of it  
as the foundation for your skyscraper) then you can start with the  
fancy mullions and whatnot of CSS.

And when you get into that end of things, you can use a tool like  
CSSEdit, which can “override” a stylesheet from your site (either  
localhost or live) and present you with a preview showing the results  
of your tinkering in real-time. No need to save, upload, etc. What you  
see is a “visual merge” between the actual HTML+CSS and HTML+CSS+your  
changes.

Walter

---

<div class="post-metadata">

**Author:** ![mohnish\_j55](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a88e4f/32.png) [@mohnish\_j55](https://www.ruby-forum.com/u/mohnish_j55)\
**Post date:** [April 13, 2011, 11:33am UTC](https://www.ruby-forum.com/t/how-to-use-javascript-onclick-functionality-via-rails-helpers/205628/5 "2011-04-13T11:33:54Z")

</div>

Walter D. wrote in post #992312:

> On Apr 12, 2011, at 2:56 AM, Mohnish J. wrote:
> 
> > > > > would look, Kindly register in [www.diasp.org](http://www.diasp.org) and search for any
> > > > 
> > > > class=“informal”\>Some other descriptive text.
> > > 
> > > If you have a profile there, kindly post your answer there so that I  
> > > html concepts,  
> > > \<%=image\_tag “investor\_groups/#{inv\_group.title}.jpg” , :width =\>  
> > > .description{  
> > > over  
> > > to it(Kindly refer:-  
> > > [http://imagepaste.nullnetwork.net/viewimage.php?id=2054](http://imagepaste.nullnetwork.net/viewimage.php?id=2054) ). I am not  
> > > sure  
> > > where I am doing what wrong that the functionality is getting affected  
> > > as a result.
> > 
> > Kindly guide me to correct my mistake(s) on this
> > 
> > Thanks..
> 
> Not sure if you’ve already sorted this out, but if I’m reading this  
> correctly, your code will be generating something similar to this cut-  
> down example:
> 
> ul  
> img/  
> li  
> some text  
> /li  
> img/  
> li  
> some text  
> /li  
> …  
> /ul
> 
> This is completely invalid HTML, so getting CSS to do anything useful  
> with it will be next to impossible.
> 
> What you must do is get that image inside the li, where it belongs,  
> and if you don’t want it to be in your autocompleter populated  
> content, then wrap the image in a span.informal. If this isn’t for  
> your autocompleter results, then you still must put the images inside  
> the li tags, but you have all sorts of tools to get the layout to work  
> the way you expect once you do that.
> 
> One of the best tools you can use to get this sort of problem sorted  
> is first to go the [validator.w3.org](http://validator.w3.org) and either put in the URL of your  
> (public) site, or paste in the View Source rendered code from your  
> browser in the Direct Input field. Either one will point out where  
> your layout fails to validate. Once you have valid HTML (think of it  
> as the foundation for your skyscraper) then you can start with the  
> fancy mullions and whatnot of CSS.
> 
> And when you get into that end of things, you can use a tool like  
> CSSEdit, which can “override” a stylesheet from your site (either  
> localhost or live) and present you with a preview showing the results  
> of your tinkering in real-time. No need to save, upload, etc. What you  
> see is a “visual merge” between the actual HTML+CSS and HTML+CSS+your  
> changes.
> 
> Walter

Hi Walter,

Based on your inputs..

This code below did the magic for me…

 \<%= javascript\_include\_tag :defaults, :cache =\> true %\> \<%=stylesheet\_link\_tag "groups"%\> \<% for inv\_group in @investor\_group2 %\> - 
\<%=image\_tag "investor\_groups/#{inv\_group.title}.jpg" , :width =\> '40', :height =\> '22', :align =\> 'left' %\>\<%=h inv\_group.title%\>(\<%=h inv\_group.activated\_members.size %\>)

\<%end%\>

and in my groups.css I added the following:-

.autocomp\_field\_width\_modify  
{  
text-align: justify;  
text-indent: 3%;  
}

Thanks for your suggestion Walter, I know I might not be exactly  
following HTML Markup standards( I have still a lot ground to cover to  
strengthen my concepts in HTML.. ). But I am just happy that finally  
this works based on the requirement.

Thanks again.. Walter..

---

<div class="post-metadata">

**Author:** ![mohnish\_j55](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a88e4f/32.png) [@mohnish\_j55](https://www.ruby-forum.com/u/mohnish_j55)\
**Post date:** [April 12, 2011, 8:56am UTC](https://www.ruby-forum.com/t/how-to-use-javascript-onclick-functionality-via-rails-helpers/205628/6 "2011-04-12T08:56:20Z")

</div>

Mohnish J. wrote in post #992221:

> Walter D. wrote in post #992117:
> 
> > On Apr 11, 2011, at 6:05 AM, Mohnish G j wrote:
> > 
> > > choice on that ..🙂 .  
> > > In case you aren’t able to get a better understanding of what I have  
> > > complete method is allowing me to modify the basic functionality to
> > > 
> > > - Group Specific Profile Pic(s) starting  
> > > displayed) 3(dummy value here)  
> > > would look, Kindly register in [www.diasp.org](http://www.diasp.org) and search for any random  
> > > \<% for inv\_group in @investor\_group2 %\>  
> > > What I need is to implement is a **on click in Rails** which will  
> > > If I can figure out how to reduce the formatting space between each of  
> > > to
> > > 
> > > Thank you..
> > 
> > [Script.aculo.us](http://Script.aculo.us) Ajax.Autocompleter has a feature for just this  
> > purpose. You can strip your entire onclick business back out, and use  
> > just plain vanilla Autocompleter. All you need to do is change a tiny  
> > thing in your Rails view that returns the autocomplete results.  
> > Everything that you don’t want to be populated into the result field  
> > when a user clicks should be wrapped in a span.informal.
> > 
> > - [image tag]IndiaSome other descriptive text.
> > 
> > If a user clicks on that option in the suggestion list, only the word  
> > India will be entered into the text field that spawned the  
> > autocompleter.
> > 
> > So in your Rails template for the autocompleter results, just wrap  
> > everything that you don’t want to be entered in a span.informal, and  
> > you can have at it. If you wanted fake columns in your results, you  
> > could do:
> > 
> > - [image tag]IndiaSome other descriptive text.
> > 
> > and use more CSS on .icon and .description to make those “columns” a  
> > particular width.
> > 
> > Walter
> 
> Hi Walter,
> 
> Thanks a lot , your solution of using the span tag did exactly what I  
> needed. I have to still play around with trying to fix the width wrt the  
> fields using CSS. I shall post queries on this , if any.
> 
> I have posted the same question on stackoverflow(

[http://stackoverflow.com/questions/5595613/implementing-a-onclick-kind-of-functionality-and-formatting-wrt-rails-partial-vie](http://stackoverflow.com/questions/5595613/implementing-a-onclick-kind-of-functionality-and-formatting-wrt-rails-partial-vie)

> ).
> 
> If you have a profile there, kindly post your answer there so that I  
> could accept the same. Or if you wouldn’t want to do it, I with your  
> permission would submit the same for it to be useful to anyone in the  
> open source community…
> 
> Thanks again..  
> Hi Walter,

I am a novice wrt sytlesheets and still don’t have a good stronghold on  
html concepts,

I tried some code below in my \_index.html.erb :-

 \<%= javascript\_include\_tag :defaults, :cache =\> true %\> \<%=stylesheet\_link\_tag "groups"%\> \<% for inv\_group in @investor\_group2 %\>

\<%=image\_tag “investor\_groups/#{inv\_group.title}.jpg” , :width =\>  
‘40’, :height =\> ‘22’, :align =\> ‘left’ %\>

- \<%=h  
inv\_group.title%\>(\<%=h inv\_group.activated\_members.size %\>)

\<%end%\>

and In my groups.css I did something like:-

.description{  
width: 40%  
}  
.icon{  
width: 40%  
}  
.

This change in the code had an affect on the auto complete functionality  
and now a particular group doesn’t get highlighted on doing a mouse over  
to it(Kindly refer:-  
[http://imagepaste.nullnetwork.net/viewimage.php?id=2054](http://imagepaste.nullnetwork.net/viewimage.php?id=2054) ). I am not sure  
where I am doing what wrong that the functionality is getting affected  
as a result.

Kindly guide me to correct my mistake(s) on this

Thanks..
