0

How could I move the javascript from application_helper.rb

Currently I have javascripts in application helper

def js_field_formatter_phone()

 result = <<JS_DATE_FIELD_PHONE
 <script language="javascript" type="text/javascript">

  function js_field_formatter_phone(inputField) {
      var valueStr = inputField.value;
      var formattedStr = "";
      for (var i=0; i<valueStr.length; i++) {
          if (/^\\d/.test(valueStr.charAt(i))) {
              formattedStr += valueStr.charAt(i);
          }
      }
      inputField.value = formattedStr;
  }
 </script>
           JS_DATE_FIELD_PHONE
  return result
end

But this code display as raw html in my views

in my view I've

    <%= js_field_formatter_phone() %>

    <input type="text"
    name="billing_phone_number_home"
    size="30"
    maxlength="20"
    tabindex="<%= get_next_tabindex(@content_data) %>"
    onchange="js_field_formatter_phone(this);"
    value="<%= @content_data['billing_phone_number_home'] %>"

2 Answers 2

1

Rails 3.1 uses the asset pipeline and puts javascripts in app/assets/javascripts. I'm not quite familiar with it yet, you can read about it here: http://guides.rubyonrails.org/asset_pipeline.html

But why is this javascript in a helper at all? It doesn't appear to use the 'result' variable at all.

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

Comments

0

Put the JavaScript in a separate file. Don't mix it in with your HTML.

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.