hellomello
hellomello

Reputation: 8587

ruby on rails: true button to boolean datatype in database?

I have a column in database with datatype boolean

class Table < ActiveRecord::Migration
  def change
    create_table :services do |t|

      t.boolean :recommend, :default => false
      t.timestamps
    end
  end
end

I want use to click a button or check yes or no to a form so when user submits a review, they would recommend and change the database to true.

How would I do such a thing?

I have a button:

<div class="field-container"><%= f.button :recommend %></div>

If user clicks on it, I'm not sure if it'll save true to database? Do I need to add more to this?

Thanks!

Upvotes: 0

Views: 2602

Answers (2)

Zero Fiber
Zero Fiber

Reputation: 4465

Since you want to have button and not a checkbox for the boolean field, you can have a checkbox which is styled like a button.

http://jsfiddle.net/zAFND/4/

and then

<%= f.check_box :recommend %>

Upvotes: 2

zeantsoi
zeantsoi

Reputation: 26193

Since you have a boolean value, you can very easily use a form_for check_box to accomplish what you're trying to do:

<%= f.check_box :recommend %>

This will generate markup for a form where the value of params[:recommend] in your controller action will be understood to be a boolean value when updating (or creating) the record through ActiveRecord.

Upvotes: 1

Related Questions