【问题标题】:List of database elements with radio_button form具有单选按钮形式的数据库元素列表
【发布时间】:2014-02-21 07:31:22
【问题描述】:

我正在尝试构建一个多项选择考试网络应用程序,以便您可以根据搜索条件检索一堆问题,然后回答问题并获得有关您是否回答正确的反馈。

我的 questions_controller 有以下方法:

def check_answer
  @question = Question.find(params[:id])
  if params[:response] == @question.answer
    @outcome = 'correct'
  else 
    @outcome = 'incorrect'
  end  
    flash[:notice] = "That is #{@outcome}."
    redirect_to :back

布局索引在哪里:

<% @questions.each do |question| %>
    <h4> <%= question.stem %></h4>
    <%= form_tag "/check_answer/#{question.id}", :method => 'get' do%>
    <%= radio_button_tag "response", "a" %> <%= question.option_a%>
        <%= radio_button_tag "response", "b" %> <%= question.option_b%>
    <%= radio_button_tag "response", "c" %> <%= question.option_c%>
    <%= radio_button_tag "response", "d" %> <%= question.option_d%>
    <%= radio_button_tag "response", "e" %> <%= question.option_e%>     
    <%= submit_tag "Check Answer" %>
<% end %>

<% if flash[:notice] %>
<div class = "notice"><%= flash[:notice] %></div>
<% end %>

但是,当我选择其中一个单选按钮时,它会在每个问题的底部返回“这是(不)正确的”。根据我只是不知道如何更改它的代码,这是有道理的,因此它仅在您单击“检查答案”框的 1 个问题的底部显示“这是正确的”。

【问题讨论】:

    标签: ruby-on-rails forms flash radio-button


    【解决方案1】:

    根据您的问题,尝试添加一段逻辑来注释结果。

    查看:

    <% @correct_field ||= nil %>
    <% @questions.each do |question| %>
      <h4> <%= question.stem %></h4>
        <%= form_tag "/check_answer/#{question.id}", :method => 'get' do%>
          <%= radio_button_tag "response", "a" %> <%= question.option_a%><% "CORRECT!" if @correct_field == "a" %>
          <%= radio_button_tag "response", "b" %> <%= question.option_b%><% "CORRECT!" if @correct_field == "b" %>
          <%= radio_button_tag "response", "c" %> <%= question.option_c%><% "CORRECT!" if @correct_field == "c" %>
          <%= radio_button_tag "response", "d" %> <%= question.option_d%><% "CORRECT!" if @correct_field == "d" %>
          <%= radio_button_tag "response", "e" %> <%= question.option_e%><% "CORRECT!" if @correct_field == "e" %>     
         <%= submit_tag "Check Answer" %>
        <% end %>
    

    控制器:

    def check_answer
      @question = Question.find(params[:id])
      if params[:response] == @question.answer
        @correct_field = @params[:response]
        @outcome = 'correct'
      else 
        @outcome = 'incorrect'
      end  
      flash[:notice] = "That is #{@outcome}."
      redirect_to :back
    

    在控制器中,只需将@correct_field 设置为params 中的任何一个解析为代表选择的字符串,如上所述。

    也就是说,我不会这样做。它感觉更像是一个应该使用部分和/或 JavaScript 和 AJAX 调用来解决的问题。希望无论如何都会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-21
      • 2018-11-18
      • 2016-11-22
      • 1970-01-01
      • 2016-04-29
      • 2021-06-12
      相关资源
      最近更新 更多