How to use Javascript onclick functionality via Rails helpers

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 …:slight_smile: .

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

.

You can also get a better understanding of what I have implemented by
referring to nullnetwork.net ,
nullnetwork.net
and nullnetwork.net .

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 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
    , 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…

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

    choice on that …:slight_smile: .
    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 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 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

    Walter D. wrote in post #992117:

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

    choice on that …:slight_smile: .
    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 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 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(

    ).

    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…

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

    would look, Kindly register in 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:-
    nullnetwork.net ). 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 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

    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 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:-
    nullnetwork.net ). 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 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…

    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 …:slight_smile: .
    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 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 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(

    ).

    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:-
    nullnetwork.net ). 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…