简体   繁体   English

Rails:使用 html 输入标签获取选中的单选按钮值

[英]Rails: Get checked radio-button value with html input tags

I have a simple structure of contest:我有一个简单的比赛结构:

  • The contest has multiple questions比赛有多个问题
  • Each question has multiple answers possible每个问题都有多个可能的答案

For this I feel like what I've created is a good structure:为此,我觉得我创建的是一个很好的结构: 在此处输入图像描述

I am trying to get the answers of a user for each question from the form in my view to my controller.我试图从我的 controller 的表单中获取用户对每个问题的答案。 Long story short, I was unable to use the <%= collection_radio_buttons... %> because the method wouldn't be right.长话短说,我无法使用<%= collection_radio_buttons... %>因为方法不对。 There is not one column in my model for each answer to each question.对于每个问题的每个答案,我的 model 中没有一列。 answer_option is not a column in my questions table it's an association because it's another table... (or would you know how to help on that?) answer_option 不是我的问题表中的一列,它是一个关联,因为它是另一个表......(或者你知道如何帮助吗?)

So I've bypassed this issue by creating loops on answers_options of each question and using html inputs and labels, like this:因此,我通过在每个问题的 answers_options 上创建循环并使用 html 输入和标签来绕过这个问题,如下所示:

<%= simple_form_for @contest, url: contest_send_quizz_path(@contest), method: :post do |f| %>
  <%= f.fields_for :questions do |q| %>
    <% @questions.each do |question| %>
      <h4 class="mt-5"><%= question.title %></h4>
      <% question.answer_options.each do |answer_option| %>
        <div class="inputGroup">
          <input type="radio" name=<%= answer_option.question_id %> value="<%= answer_option.answer %>" id=<%= answer_option.id %> />
          <label for=<%= answer_option.id %>><%= answer_option.answer %></label>
        </div>
      <% end %>
    <% end %>
  <% end %>
  <div class="mt-3">
    <span class="button-effect-2">
        <%= f.button :button, "Envoyer", type: :submit, class:"text-white" %>
    </span>
  </div>
<% end %>

However now the problem is fetching those values in the controller.但是现在问题是在 controller 中获取这些值。 It seems with this question that I'd have to get it with params[:something] and that :something being the name of the input.似乎对于这个问题,我必须使用params[:something]来获取它,并且:something是输入的名称。 Is that right?那正确吗? And now that I know that, does putting params[:name] (which is the same for all concerned radio buttons of one question) directly get the checked radio, or is there another thing to do?现在我知道了,是否放置params[:name] (对于一个问题的所有相关单选按钮都相同)直接得到选中的单选,还是有另一件事要做?

Here is what I have for now, there is stuff to ignore since the structure of the rest of my code is bigger than just the contest.这就是我现在所拥有的,因为我的代码的 rest 的结构不仅仅是比赛,所以有一些东西可以忽略。 This is in the ContestsController :这是在ContestsController中:

def show
    @contest = Contest.find(params[:id])
    authorize @contest
    @time_left = seconds_to_units(@contest.deadline - Time.now)
    @is_done =  @contest.deadline < Time.now
    if @is_done
      get_winner
    end
    @questions = @contest.questions.includes([:answer_options])
end

def send_quizz
    @contest = Contest.find(params[:contest_id])
    @questions = @contest.questions.includes([:answer_options])
    authorize @contest
    current_user.contests << @contest
    user_choice = # TODO : Get checked radio value from view
    user_contest = current_user.contests.select { |contest| contest.title == @contest.title }.first
    user_contest.questions.each do |question|
      question.user_answer = user_choice
    end
    # TODO : make sure every questions were answered before submitting request
    redirect_to contests_path
    flash[:notice] = "Ta réponse a été prise en compte"
end

So is there a way to get this value, or should I change my db structure so that each question has one column for each answer?那么有没有办法获得这个值,或者我应该改变我的数据库结构,以便每个问题的每个答案都有一个列? Or maybe another solution?或者也许是另一种解决方案? Thanks !谢谢 !


EDIT:编辑:

I also tried replacing this:我也尝试替换这个:

<input type="radio" name="<%= answer_option.question_id %>" value="<%= answer_option.answer %>" id="<%= answer_option.id %>" />
<label for="<%= answer_option.id %>"><%= answer_option.answer %></label>

With this:有了这个:

<%= q.check_box :answer_option, name:answer_option.question_id, id:answer_option.id %>
<%= q.label :answer_option, answer_option.answer, for:answer_option.id %>

And getting the value in the controller with user_choice = params[:answer_option] but when I replaced check_box with radio_button , it messed up the name, id etc. values AND I couldn't select anymore.并使用user_choice = params[:answer_option]获取 controller 中的值,但是当我用check_box替换radio_button时,它弄乱了名称、id 等值,我再也不能 select 了。


EDIT 2:编辑2:

By adding this structure:通过添加这个结构:

<%= q.radio_button :answer_option, answer_option.answer %>
<%= q.label :answer_option, answer_option.answer %>

It works (no errors), however the name is set automatically and to something not specific to each question ie contest[questions][answer_option] and the label's for is set to contest_questions_answer_option , therefore clicking on the label does not link to the checkbox.它可以工作(没有错误),但是名称是自动设置的,并且不是特定于每个问题的名称,即contest[questions][answer_option]并且标签的 for 设置为contest_questions_answer_option ,因此单击 label 不会链接到该复选框。

Managed to retrieve the value of the checkbox with this structure for the radio-buttons:设法检索具有此结构的复选框的值,用于单选按钮:

<%= q.radio_button answer_option.question_id, answer_option.answer %>
<%= q.label "#{answer_option.question_id}_#{answer_option.answer.parameterize.underscore}", answer_option.answer %>

And the controller:和 controller:

user_choices = params[:contest][:questions]
user_contest.questions.each do |question|
  question[:user_answer] = "#{user_choices[:'#{question.id}']}"
  question.save
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM