views:

177

answers:

2

can we def some ruby function (other than javascript) in onclick event for radio button in rails..

for example <%= radio_button(count, voting.vote_count, :onclick => "if voting.nominees.eql?(selected.nominees) voting.update_attribute('vote_count',voting.vote_count+1 )") can i use like this..

A: 

That's not possible without Javascript, and even if it was what would stop the user from running any arbitrary code that she wanted?

Jason stewart
+2  A: 

In this case, the Ruby is running on the server and the JavaScript is running on the browser.

By the time the click happens, the Ruby program has finished running.

The only way to get the Ruby function to run would be to make a new HTTP request to the server. The XMLHttpRequest object lets you do this from JavaScript. To achieve it without JavaScript you would need to do some trickery.

<form action="..." method="post">
  <div>
    <button type="submit"> 
      <img src="looks-like-a-radio-button.png" alt="vote">
    </button>
    <input type="hidden" name="vote" value="whatever">
  </div>
</form>
David Dorward
can i increase the vote_count without the onclick event.Actually i want to increase the vote_count when the user select the answer
sts