if @challenge.name == 'foo'
@challenge.category = 'habit'
@challenge.days_challenged = 21
@challenge.why = 'bar'
else
:days_challenged
&在:why
的_form中正确设置了foo
,但没有:category
。
<div class="challenge-category">
<input class="date-format-switcher" type="radio" value="goal" name="challenge[category]" id="challenge_category_goal">
<label for="challenge_category_goal">Goal</label>
<input class="date-format-switcher" type="radio" value="habit" name="challenge[category]" id="challenge_category_habit">
<label for="challenge_category_habit">Habit</label>
</div>
<%= f.number_field :days_challenged, class: 'day-challenge' %>
<%= f.text_area :why %>
答案 0 :(得分:1)
我假设您有目的地自定义编码单选按钮。选定的单选按钮应具有&#34;已检查&#34;,即
<input class="date-format-switcher" type="radio" value="habit"
name="challenge[category]" id="challenge_category_habit" checked />
动态设置:
<%= f.radio_button :category, 'goal', class: 'date-format-switcher' %>
<%= f.radio_button :category, 'habit', class: 'date-format-switcher' %>