将单选按钮的值保存到数据库。 rails app

时间:2017-07-05 11:08:22

标签: ruby-on-rails ruby radio-button

我在页面上添加单选按钮。 我希望用户能够选择订单是否被发送或者是否接收订单。

我很难将值保存到数据库中。

有人可以告诉我吗?

    <div class="col-md-5 pick-up-buttons" id="country_div">

    <li><%= f.radio_button :pick_up, true %></li>
    <li><%= f.label :pick_up, "Pick up order in store", :value => "false"  %></li>


    <li><%= f.radio_button :pick_up, false, :checked => true %></li>
    <li><%= f.label :pick_up, "have the order delivered", :value => "true" %></li>

  </div>    

这里是schema_file

  create_table "orders", force: :cascade do |t|
    t.string   "name"
    t.string   "email"
    t.text     "address"
    t.string   "city"
    t.string   "country"
    t.datetime "created_at",                 null: false
    t.datetime "updated_at",                 null: false
    t.boolean  "shipped",    default: false
    t.string   "pick_up"

  end

2 个答案:

答案 0 :(得分:1)

跑步:

rails generate migration changeStringToBooleanOfPickup

然后将生成的迁移文件修改为:

change_column :orders, :pick_up, 'boolean USING CAST(pick_up AS boolean)'

然后执行               rake db:migrate

答案 1 :(得分:1)

pickup列的数据类型应为布尔值以保存真/假值。

<%= f.radio_button :pick_up, "1", checked: @order.pick_up?, data: { question: 'Pick up order in store' } %> <%= f.label :pick_up, "Yes" %>
<%= f.radio_button :pick_up, "0", checked: @order.pick_up?, data: { question: 'have the order delivered?' } %> <%= f.label :pick_up, "No" %>