简化select_tag/form


在model里新建

  CHANNEL_TYPES = { 
    '信号源': :signal_source,
    '直播轮播': :live_and_rotate,
    '我的': :my_channel,
    'C 频道': :c_channel
  }

并增加方法

  def channel_type_human
    CHANNEL_TYPES.invert[channel_type.to_sym]
  end 

在view/form里可以直接用

      <div class="form-group">
        <%= f.label :channel_type,"频道类型", class: "col-sm-2 control-label" %>
        <div class="col-sm-2">
          <%= f.select :channel_type, options_for_select(LauncherChannel::CHANNEL_TYPES, @launcher_channel.channel_type),
            {}, class: "form-control" %>
        </div>
      </div>


之前没有加{}, 由于默认是四个参数的,最后一个才是html_option

Wraps ActionView::Helpers::FormOptionsHelper#select for form builders:

<%= form_for @post do |f| %>
  <%= f.select :person_id, Person.all.collect { |p| [ p.name, p.id ] }, include_blank: true %>
  <%= f.submit %>
<% end %>
Please refer to the documentation of the base helper for details.

Source: hide | on GitHub

# File actionview/lib/action_view/helpers/form_options_helper.rb, line 776
def select(method, choices = nil, options = {}, html_options = {}, &block)
  @template.select(@object_name, method, choices, objectify_options(options), @default_options.merge(html_options), &block)
end
submit(value=nil, options={})


你可能感兴趣的:(简化select_tag/form)