Mostafa Hussein
Mostafa Hussein

Reputation: 11980

Can't Run CoffeeScript file [Rails]

After Putting this code in clients.js.coffee and refreshed my browser nothing happened , then I restarted by local server and also nothing happened, What should I do ?

clients.js.coffee

jQuery ->
  $('#client_street_id').parent().hide()
  street = $('#client_street_id').html()

  $('#client_city_id').change ->
    city = $('#client_city_id :selected').text()
    escaped_city = city.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1')
    options = $(street).filter("optgroup[label='#{escaped_city}']").html()

    if options
      $('#client_street_id').html(options)
      $('#client_street_id').parent().show()
    else
      $('#client_street_id').empty()
      $('#client_street_id').parent().hide()

application.js

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require_tree .

Upvotes: 1

Views: 310

Answers (1)

rposborne
rposborne

Reputation: 802

Fixed Indentation.

Each -> should be followed by 2 spaces indent until you wish to close the function.

Also http://js2coffee.org/ for basic conversions (just to give you an idea)

jQuery ->
  $('#client_street_id').parent().hide()
  street = $('#client_street_id').html()
  $('#client_city_id').change ->
    city = $('#client_city_id :selected').text()
    escaped_city = city.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1')
    options = $(street).filter("optgroup[label='#{escaped_city}']").html()
    if options
      $('#client_street_id').html(options)
      $('#client_street_id').parent().show()
    else
      $('#client_street_id').empty()
      $('#client_street_id').parent().hide()

Upvotes: 1

Related Questions