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?

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

2 Comments

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?
The problem is that both string and array is allowed for class
3

Why not use jquery, somthing like:

$('button').attr('class', 'blah');

3 Comments

That's not a bad idea actually. There must be a simple way to do this in Rails though, surely.
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.
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.