【问题标题】:Rails: Get checked radio-button value with html input tagsRails:使用 html 输入标签获取选中的单选按钮值
【发布时间】:2020-08-19 04:38:07
【问题描述】:

我有一个简单的比赛结构:

  • 比赛有多个问题
  • 每个问题都有多个答案 可能

为此,我觉得我创建的是一个很好的结构:

我试图从我视图中的表单到我的控制器中获取用户对每个问题的答案。长话短说,我无法使用<%= collection_radio_buttons ... %>,因为方法不对。对于每个问题的每个答案,我的模型中都没有一列。 answer_option 不是我的问题表中的一列,它是一个关联,因为它是另一个表...(或者您知道如何提供帮助吗?)

所以我通过在每个问题的 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 %>

但是现在的问题是在控制器中获取这些值。似乎this question 必须使用params[:something] 来获取它,而:something 是输入的名称。那正确吗?现在我知道了,是否将params[:name](对于一个问题的所有相关单选按钮都相同)直接得到选中的单选,或者还有其他事情要做吗?

这就是我现在所拥有的,有一些东西可以忽略,因为我其余代码的结构不仅仅是比赛。这是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

那么有没有办法获得这个值,或者我应该改变我的数据库结构,让每个问题的每个答案都有一列?或者也许是另一种解决方案?谢谢!


编辑:

我也试过替换这个:

<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>

有了这个:

<%= 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 %>

然后用user_choice = params[:answer_option] 获取控制器中的值,但是当我用radio_button 替换check_box 时,它弄乱了名称、id 等值,我无法再选择了。


编辑 2:

通过添加这个结构:

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

它可以工作(没有错误),但是名称是自动设置的,并且不是特定于每个问题的名称,即 contest[questions][answer_option] 并且标签的 for 设置为 contest_questions_answer_option,因此单击标签不会链接到复选框.

【问题讨论】:

    标签: ruby-on-rails parameters radio-button simple-form


    【解决方案1】:

    设法检索具有此结构的单选按钮复选框的值:

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

    还有控制器:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-20
      • 2012-03-10
      • 1970-01-01
      • 2012-10-27
      • 2016-05-21
      • 2013-12-11
      • 1970-01-01
      • 2020-05-07
      相关资源
      最近更新 更多