簡體   English   中英

Rails:如何從單選按鈕獲取布爾數據?

[英]Rails : How can I get boolean data from radio button?

我有數據庫表“問題”,“答案”。(這兩個表已通過has_many,belongs_to和nested_form關系關聯。)

並且答案表中有user_answer(boolean,default'false')列。

我要這樣

  1. 如果用戶選中單選按鈕,則user_answer更改為“ true”值。
  2. 然后,我將在答案表的正確列中比較另一個值,
  3. 最后,我將結果保存在問題表的is_correct列中。

但是我不知道該怎么辦。 這是輸入表格。

<h1><%= @survey.name %></h1>
 <%= form_tag({:controller => "surveys", :action => "grading"}) do %>
  <ol class="questions">
   <% @survey.questions.each do |question| %>
   <li>
   <strong><%= question.content %></strong>
    <ol class="checkbox">
     <% question.answers.each do |answer| %>
       <%= radio_button_tag(answer.user_answer) %>
       <%= label("answer_".concat(answer.id.to_s).to_sym, answer.content) %>
     <% end %>
     </ol>
     <hr />
   </li>
   <% end %>
  </ol>

  <div><%= submit_tag("Submit", :class => "submit") %></div>

當然,有一個錯誤。 radio_button_tag需要2個參數。 但是我不知道該怎么辦。 請告訴我。

在模型中聲明一個常量,例如:

STATUS = [['active', true], ['Inactive', false]]

並在視圖中使用選項進行選擇。

<%= f.select :status, options_for_select(User::STATUS) %>

有用的資源:

http://apidock.com/rails/ActionView/Helpers/FormTagHelper/radio_button_tag

http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_for_select

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM