infinity
infinity

Reputation: 719

Send helper additional class

In a rails 4 app, I'm trying to pass a default option to the text_field helper, but seem to be stuck on how to implement this.

So far, I have in my view:

<%= new_text_field :name, class: "", placeholder: "" %>

and in my application_helper.rb

def new_text_field(object_name, method, options = {})
   text_field(object_name, method, options = {}) # Trying to pass in a default class here, for example ".bigger"
end

Upvotes: 0

Views: 30

Answers (2)

Surya
Surya

Reputation: 16022

Try this:

def new_text_field(object_name, method = nil, options = {})
  options[:class] ||= 'bigger' # this will set bigger as default value if "class" option isn't passed
  text_field(object_name, method, options = {})
end

Upvotes: 1

SHS
SHS

Reputation: 7744

Something like this should work:

def new_text_field_tag(name, value=nil, options)
  your_class = "bigger"

  if options.has_key?(:class)
    options[:class] += " #{your_class}"
  else
    options[:class] = your_class
  end

  text_field_tag(name, value, options)
end

Upvotes: 1

Related Questions