模型范围的默认单选按钮?

时间:2016-02-11 17:42:12

标签: ruby-on-rails ruby model-view-controller scope radio-button

我可以使One-Shot选择默认单选按钮的最佳方式是什么?

_form

<% Challenge::CATEGORY.each do |c| %>&nbsp;
  <span class="label label-primary"> <%= label(c, c) %> </span>
  <%= f.radio_button(:category, c, :class => "date-format-switcher") %>
<% end %>

<div id='id_of_first_div'>
  One-Shot
</div>

<div id='id_of_second_div'>
  Ongoing
</div>

<script>
$(function(){
  $('#challenge_category_one-shot').click(function(){ $('#id_of_first_div').show(); $('#id_of_second_div').hide(); });
  $('#challenge_category_ongoing').click(function(){ $('#id_of_first_div').hide(); $('#id_of_second_div').show(); });
});
</script>

challenge.rb

scope :oneshot,  -> { where(categories: 'One-Shot') }
scope :ongoing,  -> { where(categories: 'Ongoing') }
CATEGORY = ['One-Shot', 'Ongoing']

模式

t.string   "category"

1 个答案:

答案 0 :(得分:1)

如果您打算使用f.radio_button,则可以添加参数

checked: (c=='One-Shot')

您也可以使用r.collection_radio_buttons代替:

<% categories = ['One-Shot', 'Ongoing'] %>                                    
<%= f.collection_radio_buttons :category, categories, :to_s, :to_s, {checked: 'One-Shot'} %>