发送助手附加课程

时间:2014-12-29 03:04:44

标签: ruby-on-rails ruby

在rails 4应用程序中,我正在尝试将默认选项传递给text_field帮助程序,但似乎不知道如何实现它。

到目前为止,我认为:

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

和我的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

2 个答案:

答案 0 :(得分:1)

这样的事情应该有效:

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

答案 1 :(得分:1)

试试这个:

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