Simple Rails helper for using OnClick on div, tr and other tags

I came across the issue of making set of div tags’ onClick event to be connected with a remote function, yet I couldn’t find a helper method to that and I made a simple and stupid helper method by cannibalizing link_to_remote and link_to_function helper methods.

usage: just like using link_to_remote, yet we don’t specify “name”, since there’s no text going inside the tag except the onclick event info

//e.g:
<<%= onclick_to_remote :url => my_restful_url %>; class="my_1">;

I works for me and get the work done.. arguments such as :with are supported.. Posting it here hoping someone will find it useful or may be improve it as well.
check out the pastie for better viewing pleasure and understanding http://pastie.caboo.se/200546

def onclick_to_remote(options = {})
*args = remote_function(options)
function = args[0] || ''
function = update_page(&amp;block) if block_given?
"onclick=\"#{function}; return false;\""
end

8 Comments For This Post

  1. Anonymous Says:

    hi

  2. Anonymous Says:

    hi daredevil
    I use your snippet in a tr tag in order to modify a standard rails scaffold.
    Infact the click event fired the action I wish.
    but the problem is that the html output doesn’t render on browser! I can only read it through mozilla firebug. (???)
    Any idea?
    thanks in advance.
    Andrea

  3. DareDevil Says:

    Hi Andrea,

    I think the best way to verify if there’s something wrong with the snippet is to call a simple method without any other ajax functions ie

    page.alert “Snipped Working”

    So you can separate the issue. Also check your development logs so you can see if there are any errors. tip: If you use mongrel in development env using command line you can see.

    if possible send further info and i’ll try to check it out and see if i can help in some way (even though i’m not a rails guru :)

  4. Anonymous Says:

    hi daredevil
    I develop with netbeans 6.0.1 so I use webbrick as web server.
    I’ve try your suggestions but:
    on development logs Rails records my attemps without error just like the rendering really happens. o_O

    I put the page.elert instead the action I wish to renderize with onclick event but nothing change…

    could I ask to you if you ever try to fired the standard scaffold ‘edit’ action with your snippet instead with the standard ‘link_to’ method?
    thanks
    Andrea

  5. DareDevil Says:

    Andrea,

    sorry for the late reply.. i think it got to do something with the browser.. did you try with IE (windows) or Galeon (Linux)…

    Try to set up a very simple controller and methods just to check that. i’ll try to send you some sample project if i get a chance. Sorry i got no experience with scaffolding though…

  6. Anonymous Says:

    hi daredevil
    thank you for the attention.
    anyway scaffolding is the simplest rails tecnique to build a simplest data table manager (crud management).
    any newby start to develop rails app. with scaffold wizard.
    infact I’m a newby! :D
    I hope you will so kind to show a sample about you snippet.
    thank you again
    Andrea

  7. Anonymous Says:

    hi daredevil
    At the end I’ve solved my problem!
    simply implemented in the tr tag the normal onclick event syntax with the url_for method.

    I’m sure I was not clear to explain my problem to you: it was just the needed to select by a data table a single row in order to editing it.
    thank you for your help anyway
    bye
    Andrea

  8. uroxatral 10mg Says:

    I have to…
    —————————————
    signature: order lopid

Leave a Reply