Ask A Question

Notifications

You’re not receiving notifications from this thread.

Just want to change button text on-click. Coffeescript not making any sense to me.

NicWickman asked in Javascript
Hey guys, trying to do a pretty simple thing but all the information I can find online (for "beginners") seems to omit some core things I'm expected to know, because it doesn't even do so much as throw an error. Rails 5.2

I just want to change the text of my button on click.

I have a standard button_to
    <%= button_to "Unsave", resource_path(id: resource.id), remote: true, method: :delete, class: "save_btn", id: "save_btn"%>

I have saved the following to "resources.coffee" in my javascripts folder
$ ->
  $(".save_btn").on "ajax:success", (e, data, status, xhr) ->
   $('.save_btn').text('Save')

In my application.html.erb layout I am loading my JS
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <%= stylesheet_link_tag    'application', media: 'all',
                                              'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'resources', 'data-turbolinks-track': 'reload' %>

Rails threw me an error and instructed me to insert this into assets.rb, which I have done
Rails.application.config.assets.precompile += %w( resources.js )

Nothing happens to the button when I click it. Can somebody please fill in the blanks for me? I have no javascript experience and I am pretty sure I am missing something to the equivalent of "Is it plugged in?".
Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,329+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2024 GoRails, LLC. All rights reserved.