0
我在表單中使用自定義select_with_new_option
方法是這樣的:Rails:如何用「div」標籤包裝生成的「select」?
<%= select_with_new_option(f, :shop, :name, :id) %>
下面是它的定義:
def select_options_with_create_new(objects, text_field, value_field, options={})
object = objects.to_s.singularize
all_objects = object.capitalize.constantize.all
all_objects = all_objects.sort_by(&options[:order_by]) if options.has_key?(:order_by)
select_options = all_objects.map{ |obj| [obj.send(text_field), obj.send(value_field)] }
select_options << [wrap_create_new_option("create new #{object}".titleize), "new_#{object}"]
end
def wrap_create_new_option(str)
">> #{str} <<"
end
# By default, sorts by 'text_field'.
def select_with_new_option(f, object, text_field, value_field)
f.select(:"#{object}_id", select_options_with_create_new(object.pluralize, text_field, value_field, :order_by => text_field),
{:prompt => true}, # options
{}) # html_options
end
這就造成了select
標籤和它的option
S作爲預期。
現在,我想用一個div
這樣來包裝這個select
:
<div class='my_class'>
<select>...</select>
</div>
什麼將是「Rails的方式」做最簡單的方法?