2

I'm starting to use the jQuery UI CSS Framework for an app, which means I have to start adding classes to everything. So, for example, I want to make all buttons jQuery-themed, which means adding a class to all buttons.

I imagine there's some way in Rails to modify the helpers so I don't have to manually add a :class => 'blah' to every button, but I can't work it out. Is this possible, or does anybody have any better ideas?

Skilldrick
  • 69,215
  • 34
  • 177
  • 229

2 Answers2

5

You just need override the helper method with this class add. In your application_helper.rb

  def button_to(name, options = {}, html_options = {})
    super(name, options, html_options.merge(:class => 'blah'))
  end
Skilldrick
  • 69,215
  • 34
  • 177
  • 229
shingara
  • 46,608
  • 11
  • 99
  • 105
  • Is there a way you can specify more classes, apart from the default 'blah' class ? edit: I used `html_options.merge(:class => (html_options[:class] || '') + ' blah')` is that ok to use? – Cristian Sep 22 '11 at 14:55
  • The problem is that both string and array is allowed for class – GorillaApe Nov 16 '17 at 09:49
3

Why not use jquery, somthing like:

$('button').attr('class', 'blah');
coder_tim
  • 1,710
  • 1
  • 12
  • 13
  • That's not a bad idea actually. There must be a simple way to do this in Rails though, surely. – Skilldrick Sep 29 '10 at 21:38
  • I've done some more reading and it looks like this may be the most sensible answer - it doesn't look like a simple task to override helper methods. I'll mark this accepted unless I get a Rails-based solution today. – Skilldrick Sep 30 '10 at 07:06
  • I actually went this route in the end - the helper way had too many edge cases, and I'm not writing all my HTML with helpers anyway. The jQuery way keeps it all in one place with one technique. I don't really feel like I can mark this as the accepted answer though, because the other one answers the question I actually asked. But thanks anyway! – Skilldrick Sep 30 '10 at 20:50