Fernando Maymone
Fernando Maymone

Reputation: 355

How to load a coffeescript on my views in rails?

Im stuck with a very basic stuff here at my application.

I have a form that I want to make some processing using coffeescript.

My controller/action isProfilesController#edit_profile_photos

And my view is: /profile/edit/profile_photos.html.erb

<span class="file-upload">
    <div class="progress" id='progress-bar' style='display:none;'>
    <div class="progress-bar progress-bar-striped active" role="progressbar" style="width: 0%">
    <span class="sr-only" id='progress-bar-text'></span>
    </div>
</div>
<button type="button" class="btn btn-lg btn-outline-green trigger-file-upload">Upload a new photo</button>
<%= f.attachment_field :avatar, direct: true, presigned: true %>
</span>

So, I created a users.coffee file with this:

$(document).ready ->
   alert "page has loaded!"

 jQuery ->
  $(document).on "upload:start", "form", (e) ->
    $(this).find("input[type=submit]").attr "disabled", true
    $("#progress-bar").slideDown('fast')

  $(document).on "upload:progress", "form", (e) ->
    detail          = e.originalEvent.detail
    percentComplete = Math.round(detail.loaded / detail.total * 100)
    $('.progress-bar').width("#{percentComplete}%");
    $("#progress-bar-text").text("#{percentComplete}% Complete")

  $(document).on "upload:success", "form", (e) ->
    $(this).find("input[type=submit]").removeAttr "disabled"  unless $(this).find("input.uploading").length
    $("#progress-bar").slideUp('fast')

Well. I dont know why my coffee file its not being loaded. Should it have the same name of the controller? (profiles.coffee)?

Thanks

Upvotes: 0

Views: 710

Answers (1)

xploshioOn
xploshioOn

Reputation: 4115

you can add it to the application.js file to include it to load, I am assuming you have the standard configuration on layouts, loading application.js there.

//= require users

or you can name the file as the controller, and add this to your layout

<%= javascript_include_tag params[:controller] %> 

you have a lot of options to solve it, if this doesn't help you, please add more info to the question, like application.js and layout/application.html.erb

Upvotes: 1

Related Questions