0

I have a form with 3 input fields, strokes, putts, and GIR:

<%= form_for(@played_hole) do |f| %>

  <%= f.hidden_field :hole_par, value: @hole.par %>

  <%= f.label :strokes, "How many strokes?" %>
  <%= f.select(:strokes, options_for_select(generate_strokes_array(@hole.par), selected: @hole.par)) %>

  <%= f.label :putts, "How many putts?" %>
  <%= f.select :putts, options_for_select(generate_putts_array(5), selected: 1), onclick: calculateGIR() %>    

  <%= f.label :GIR, "Green in Regulation?" %>
  <%= f.select(:GIR, [['Yes', 1], ['No', 0]]) %>

  <%= f.submit "Next Hole", class: "btn btn-large btn-success" %>

<% end %>

My goal is to automatically select the 3rd field (:GIR) once the user clicks on the second input, putts. For example, if the hole is a par 4 and they selected 5 in the :strokes field and 2 in the :putts field, then I would know that they didn't get a Green In Regulation. So, I'd like to automatically switch the :GIR select to 'No'.

I'm currently having trouble just getting an onclick() event registered. I've added onclick: calculateGIR() to the :putts select field but I'm getting this error in my PlayedHole#new view: undefined method calculateGIR' for #<#<Class:0xb5a684c4>:0xb591eac8>

I've added this to the javascript/played_holes.js file: function calculateGIR() { alert("calculating GIR"); } but obviously it's not being picked up.

Basically, I could use some help calling javascript (or jQuery) functions within a view. And then once I get that setup I'd like to figure out how to pull the selected value from :strokes and :putts to calculate the GIR.

For those curious, a GIR means you were on the green in 2 (or less) strokes than the par for the hole. i.e. Par 3 - 1 stroke, Par 4 - 2 strokes, Par 5 - 3 (or less) strokes.

And I'm running Rails 3.2, in case you're curious.

Thanks!

1
  • Try: <%= f.select :putts, options_for_select(generate_putts_array(5), selected: 1), onclick: 'calculateGIR()' %>, put the calculateGIR() in quotes. Commented Sep 6, 2013 at 20:57

2 Answers 2

1

You need to put calculateGIR() in quotes:

<%= f.select :putts, options_for_select(generate_putts_array(5), selected: 1), onclick: "calculateGIR()" %> 

The onclick HTML attribute should be a a string of JavaScript code that will be executed on click. Instead, you are trying to call a Ruby method, calculateGIR.

Sign up to request clarification or add additional context in comments.

Comments

0

Alternative to my comment in your question:

Try: <%= f.select :putts, options_for_select(generate_putts_array(5), selected: 1), onclick: 'calculateGIR()' %>, put calculateGIR() in quotes.

It's better not to place javascript into your HTML as you've shown. Unobstrusive way to do this would be to assign an id to the select element and apply calculateGIR logic in the change event.

Something like following is better:

In your view:

<%= f.select :putts, options_for_select(generate_putts_array(5), selected: 1), {}, id: 'putts' %>

In your javascript/played_holes.js

$(document).ready(function() {
  $('#putts').on('change', function() {
     // Place definition of calculateGIR() here.
  });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.