铁路单选按钮值未提交

时间:2016-07-03 16:34:27

标签: ruby-on-rails

我的rails应用程序中有一个应该是简单的测验。模型为ManTest,数据表中的唯一列为scoreuser的引用关系。我的表单有点工作,但无论选择了哪个单选按钮,它都会将score记录为0。

我的测验的格式为_test0.html.erb

<%= form_for @man_test do |f| %>
    <h3>
      <%= f.label "Which can you NOT score when playing American football?" %>
      <%= f.radio_button :score, value: "1" %>Touchdown<br>
      <%= f.radio_button :score, value: "0" %>Basket<br>
      <%= f.radio_button :score, value: "1" %>Field Goal<br>
      <%= f.radio_button :score, value: "1" %>Safety</h3>
      <div class="text-center">
        <%= f.submit "Confirm I'm a Man", class: "btn-boring" %>
      </div>
    </h3>
<% end %>

man_tests#new页面上这样调用:

<div class="hover-well col-xs-10 col-xs-push-1 col-sm-8 col-sm-push-2">
  <h1>Answer the following question to gain entry:</h1>
  <%= render partial: 'man_tests/test00' %>
</div> <!-- hover-well -->

当我在我的控制台中ManTest.last时,我得到了这个:

[3] pry(main)> ManTest.last
  ManTest Load (0.3ms)  SELECT  "man_tests".* FROM "man_tests"  ORDER BY "man_tests"."id" DESC LIMIT 1
=> #<ManTest:0x007f851acabdd0
 id: 3,
 score: 0,  <<<<< SCORE IS ZERO REGARDLESS OF RADIO SELECTED
 user_id: nil,
 created_at: Sun, 03 Jul 2016 04:28:55 UTC +00:00,
 updated_at: Sun, 03 Jul 2016 04:28:55 UTC +00:00>

任何人都可以帮我找出导致所有选择得分为零的错误吗?

其他信息

这里是ManTest表的架构:

  create_table "man_tests", force: :cascade do |t|
    t.integer  "score"
    t.integer  "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

这是man_tests_controller

class ManTestsController < ApplicationController
  layout false

  def new
    @man_test = ManTest.new
  end

  def create
    @man_test = ManTest.new(man_test_params)
    if @man_test.save && @man_test.score == 1
      redirect_to home_index_path
    elsif @man_test.save && @man_test.score == 0
      redirect_to controller: :man_tests, action: :fail
    else
      render controller: :man_tests, action: :new
    end
  end

  def fail
  end

  private

  def man_test_params
    params.require(:man_test).permit(:user, :score)
  end
end

1 个答案:

答案 0 :(得分:0)

我最终改变了测验的erb,它的工作原理如下:

  <%= f.label "Which can you NOT score when playing American football?" %>
  <%= f.radio_button :score, 0 %>
  <%= f.label :score, "Touchdown", value: 0 %><br>
  <%= f.radio_button :score, 1 %>
  <%= f.label :score, "Basket", value: 1 %><br>
  <%= f.radio_button :score, 0 %>
  <%= f.label :score, "Field Goal", value: 0 %><br>
  <%= f.radio_button :score, 0 %>
  <%= f.label :score, "Safety", value: 0  %><br>