0

I'm adding radio buttons to my page. I want the user to be able to pick whether the order gets sent or they pick it up.

I'm having troubles to save the values to the database.

can someone advise me?

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

here is the 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
5
  • that should be the type boolean Commented Jul 5, 2017 at 11:11
  • ok... is there a way to change it? Commented Jul 5, 2017 at 11:12
  • you need to run another migration and update the datatype of field Commented Jul 5, 2017 at 11:16
  • if you want then i can give you the solutions Commented Jul 5, 2017 at 11:21
  • Thank you, that would be very nice of you Commented Jul 5, 2017 at 11:22

2 Answers 2

1

Just run:

rails generate migration changeStringToBooleanOfPickup

then modified the generated migration file to:

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

then execute rake db:migrate

Sign up to request clarification or add additional context in comments.

3 Comments

I get this error messages when I do rake db:migrate PG::DatatypeMismatch: ERROR: column "pick_up" cannot be cast automatically to type boolean HINT: You might need to specify "USING pick_up::boolean". : ALTER TABLE "orders" ALTER COLUMN "pick_up" TYPE boolean
fixed it by doing = def change change_column :orders, :pick_up, 'boolean USING CAST(pick_up AS boolean)' end
updated the answer yes you can do it by using it,is it working now?
1

Datatype of pickup column should be boolean to save true/false value.

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

4 Comments

thak you for that but, how would I turn this to have the 'have the order delivered?' default checked?
this might sound stupid, but were should the value "1"be inserted to the code? I've tried to inserted but always get an error
try this: <%= f.radio_button :pick_up, "1", checked: @order.pick_up?, data: { question: 'have the order delivered?' } %> <%= f.label :pick_up, "No" %>
try <%= f.radio_button :pick_up, "1", checked: true, data: { question: 'have the order delivered?' } %> <%= f.label :pick_up, "No" %>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.