Ruby on rails 如何为我的测验应用程序布局表单以及发布到哪里?
我想我的问题有两方面:Ruby on rails 如何为我的测验应用程序布局表单以及发布到哪里?,ruby-on-rails,Ruby On Rails,我想我的问题有两方面: 现在我只是显示问题和答案,但用户没有选择的选项(正确答案部分只是测试我是否可以正确设置哪些答案是正确的)。我有调查>问题>答案的嵌套模型 调查/show.html.erb: <ol> <% @survey.questions.each do |question| %> <li class = 'question'><%= question.content %> <ul> <% for
<ol>
<% @survey.questions.each do |question| %>
<li class = 'question'><%= question.content %>
<ul>
<% for answer in question.answers %>
<li class = 'answer'>
<%= answer.content %>
<%if answer.correct_answer == true %>
<span> <b>Correct Answer</b> </span>
<% else %>
<span> <b>Incorrect Answer</b></span>
<% end%>
</li>
<% end %>
</ul>
</li>
<% end %>
</ol>
-
正确答案
答错了
等价形式是什么样的
基本上,我会有一个
QuestionsController
和AnswersController
这是因为我知道以后可能会有其他事情我想通过HTTP进行问答。
我还有一个存储答案、问题和用户的表格,你可以称之为已回答的问题
class AnsweredQuestion
belongs_to :user
belongs_to :question
belongs_to :answer
end
class User
has_many :answered_questions
end
class Question
has_many :answered_questions
end
class Answer
has_many :answered_questions
end
class Survey
has_many :questions
has_many :answered_questions, through: :questions
accepts_nested_attributes_for :answered_questions
end
class SurveysController
def answer_question
@survey = Survey.find(params[:id])
@survey.update(answer_question_params) # not sure this is best way here, need to research a better way to create the nested resources rather than update
end
private
def answer_question_params
params.require(:survey).permit(answered_questions_attributes: [:answer_id, :question_id, :user_id])
end
end
然后,为了让您的用户选择答案,我有如下内容:
<ol>
<%= form_for @survey, url: survey_answer_questions_path do |f| %>
<% @survey.questions.each do |question| %>
<li class = 'question'><%= question.content %>
<ul>
<%= f.fields_for :answered_questions do |aq| %>
<%= aq.hidden_field :question, question.id %>
<%= aq.hidden_field :user, current_user.id %>
<% question.answers.each do |answer| %>
<%= aq.radio_button :answer, answer.id, answer.correct_answer %>
<%= aq.label :answer, answer.content %>
<% end %>
<% end %>
</ul>
</li>
<%= f.submit_button "Answer" %>
<% end %>
</ol>
我正在进行动态测试,无法真正测试这些是否都正确,但它显示了我将采取的总体概述/方法
如果有帮助,请告诉我。为什么您决定将答案和回答问题分开?因为我猜您的应用程序将有多个用户,并且您可能希望向多个用户显示相同的问题。除非你有足够的带宽为你的应用程序的每个用户创建多个问题和答案,谢谢你的回答,但我仍然有一大堆的麻烦弄明白这一点。我将发布另一个问题,更好地涵盖我正在努力解决的问题。