Rails - 帮助在 RoR 中进行在线测验评分

发布于 2024-09-03 01:42:09 字数 1075 浏览 4 评论 0原文

我正在尝试对我想做的测验应用程序进行评分。我有一个问题模型,包含并询问(实际问题)、4 个选择(广告)和一个正确答案(字符串)。

在视图中,我显示了 4 个问题,然后是正确的答案选择(这只是功能测试),然后我创建了一个 text_field 来接受用户的答案选择和一个按钮来刷新具有评分逻辑的索引操作,现在..

--我需要将text_field放在form_tag中吗?

<p>1. <%= h @question.q1  %></p>
<p>2. <%= h @question.q2  %></p>
<p>3. <%= h @question.q3  %></p>
<p>4. <%= h @question.q4  %></p>
<p>Answer: <%= h @question.correct  %></p>
<%= text_field_tag :choice, params[:choice] %> 
<%= button_to "Grade", {:controller => 'site', :action => "index"}  %> 
<p> <%= @answer %></p>

这是索引控制器操作,

def index 
      @question = Question.find(1) 
         if @question.correct == params[:choice]
             @answer = 'right'
         else
                @answer = 'wrong'
         end
end

它并没有真正起作用。文本字段应该选择“a”或“c”等字母,并将其与数据库中的正确答案进行比较。

我希望它能通过单选按钮工作,但我是 Rails 的新手,所以我想我应该采取一些小步骤。

因此,如果有人可以帮助我如何通过字符串或最好通过单选按钮来解决此问题,我将非常感激。

I'm trying to grade a quiz application I would like to make. I have a questions model with and ask(the actual question), 4 choices(a-d), and a correct answer(string).

In the view I have the 4 question being diplayed then the correct answer choice (This is just a test for functionality) and then I created a text_field to accept the users answer choice and a button to refresh the index action which has the scoring logic, for now..

--Do I need to put the text_field within a form_tag?

<p>1. <%= h @question.q1  %></p>
<p>2. <%= h @question.q2  %></p>
<p>3. <%= h @question.q3  %></p>
<p>4. <%= h @question.q4  %></p>
<p>Answer: <%= h @question.correct  %></p>
<%= text_field_tag :choice, params[:choice] %> 
<%= button_to "Grade", {:controller => 'site', :action => "index"}  %> 
<p> <%= @answer %></p>

Heres the index controller action

def index 
      @question = Question.find(1) 
         if @question.correct == params[:choice]
             @answer = 'right'
         else
                @answer = 'wrong'
         end
end

Its not really working. The textfield is supposed to take a letter choice like 'a' or 'c' and compare it with the correct answer in the database.

I would love this to work by radiobuttons, but I'm a newbie to rails so I thought I'd take baby steps.

So if anyone can help me with how to fix this by string, or preferably by radiobuttons, I'd really appreciate it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪痕残 2024-09-10 01:42:09

我的做法如下:

# in app/controller/QuestionsController
def index 
  @question = Question.find(1) 
  @grade = params[:choice] == @question.answer ? 'PASS' : 'FAIL'
end

它需要在您的 config/routes.rb 文件中指定一个命名路由:

map.questions 'questions', :controller => 'questions', :action => 'index'

然后在 app/views/index.html.erb 中:

<h2><%=h @question.question_text %></h2>

<ol>
  <li><%=h @question.q1 %></li>
  <li><%=h @question.q2 %></li>
  <li><%=h @question.q3 %></li>
  <li><%=h @question.q4 %></li>
</ol>

<p>
  Correct answer <%=h @question.correct %>
</p>

<% form_tag do %>
  <p>Choice? <%= text_field_tag :choice %></p>
  <%= submit_tag 'Grade' %>
<% end %>

<p>Grade: <%= @grade %></p>

如果您提供routes.rb 文件以及控制器代码的其余部分,我可以为您提供更具体的帮助。上面的答案根本不是 RESTful 的。如果这对您来说很重要,那么路由配置以及控制器代码都会有所不同。此外,通过 RESTful 设计,您可以在视图中使用 form_for 方法调用,这在当今更为标准。

Here's how I would do it:

# in app/controller/QuestionsController
def index 
  @question = Question.find(1) 
  @grade = params[:choice] == @question.answer ? 'PASS' : 'FAIL'
end

It will require a named route in your config/routes.rb file:

map.questions 'questions', :controller => 'questions', :action => 'index'

and then, in app/views/index.html.erb:

<h2><%=h @question.question_text %></h2>

<ol>
  <li><%=h @question.q1 %></li>
  <li><%=h @question.q2 %></li>
  <li><%=h @question.q3 %></li>
  <li><%=h @question.q4 %></li>
</ol>

<p>
  Correct answer <%=h @question.correct %>
</p>

<% form_tag do %>
  <p>Choice? <%= text_field_tag :choice %></p>
  <%= submit_tag 'Grade' %>
<% end %>

<p>Grade: <%= @grade %></p>

I could give you much more specific assistance if you'd provide your routes.rb file as well as the rest of your controller code. The above answer is not RESTful at all. If that is at all important to you, the routes configuration would be different, as well as the controller code. Also, with RESTful design, you could use a form_for method call in your view, which is more standard these days.

挽容 2024-09-10 01:42:09

目前您的代码将忽略输入的文本。用户输入需要发布(通过表单和提交按钮)或通过 get(也可以使用表单完成)。

在这种情况下,我建议您将其放在表单标签中并添加一个提交按钮。将必要的操作添加到控制器(在这种情况下我相信保存),验证数据,然后渲染索引操作。

At the moment your code will ignore the text entered. User input needs to be either posted (via a form & submit button) or via a get (can also be done with a form).

In this case, I'd suggest you put it in the form tag and add a submit button. Add the necessary action to your controller (save I believe in this case), validate the data, and then render the index action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文