1

I'm just starting out in Ruby on Rails so sorry for the n00bish Q…

I want to make a button that triggers some javascript. I found this Q that lead me to writing the following code:

<%= button_to_function "Change Password", "$(this).toggle();", :class =>"password_div" %>

(Elsewhere, I have <div class="password_div" style="display: none;">…</div> tags.)

The issue is that when I run this code I get the following error:

undefined method `button_to_function' for #<<Class:0x00000006abc1c8>:0x0000000647ee00>

Which I find confusing. My impression is that this comes standard in Rails (it is listed here along with lots of other methods that as far as I can tell are available immediately in RoR).

Do I need to make JavaScript Helper available somewhere? Or am I missing something somewhere else?

Community
  • 1
  • 1
Jo.P
  • 1,139
  • 4
  • 15
  • 35
  • 2
    The official [API docs](http://api.rubyonrails.org) don't know about `button_to_function` and the comments on the linked question indicate that it went away in Rails4. Perhaps the guides are out of date. In any case, you'd be better off creating a ` – mu is too short Jun 04 '15 at 23:59
  • Ah, ok…thanks! When you say "bind a handler to it separately", you mean to make regular old javascript handler? Nothing in RoR should/need be done? – Jo.P Jun 05 '15 at 00:00
  • 1
    That's correct. I strongly suggest you read through the Rails book before going further. – Michael Chaney Jun 05 '15 at 00:04
  • Right, `button_to_function` probably went away because it was some nasty old-school ` – mu is too short Jun 05 '15 at 01:09

1 Answers1

2

It looks like it's been removed (Source code):

  def button_to_function(name, function=nil, html_options={})
    message = "button_to_function is deprecated and will be removed from Rails 4.1. We recommend using Unobtrusive JavaScript instead. " +
      "See http://guides.rubyonrails.org/working_with_javascript_in_rails.html#unobtrusive-javascript"
    ActiveSupport::Deprecation.warn message

    onclick = "#{"#{html_options[:onclick]}; " if html_options[:onclick]}#{function};"

    tag(:input, html_options.merge(:type => 'button', :value => name, :onclick => onclick))
  end
creativereason
  • 1,524
  • 1
  • 11
  • 20