How to define boolean field for a rails migration
Yes, you can use :boolean
for this, and yes it will also save database space.
Rails database boolean values
I believe that you might be looking for ActiveRecord::Base.connection.quoted_true
This returns native boolean values in quotes, e.g. '1' for SQL Server or MySQL, and 't' for PostgreSQL or SQLite
ruby on rails: true button to boolean datatype in database?
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 %>
Allow a default false boolean from database to change once button is clicked
I know I'm going to have to make a form that will do a POST/PATCH
request but i'm unsure on how to change that value.
You don't need a form here. As you are just trying to update a model instance with a button click, you just need a link_to
with a route
that is defined to perform this action. Considering your model is Request
,the below should work
<%= link_to "Accept", accept_request_path(request) %>
Where request
is the instance which need to be updated.
#routes.rb
patch /requests/:id/accept, to: requests#accept, as: :accept_request
or if you want a resourceful route, you can do
resources :requests do
patch 'accept', on: :member
end
And in the requests_controller
have a accept
method with below code
def accept
@request = Request.find(params[:id])
@request.update_attribute(accepted: true)
end
Displaying boolean values from database in the view with Rails
This can be achieved by using a helper. The first argument after the "?" is the true case; the false case follows the ":".
The example below uses glyphicons, since you mentioned wanting a check mark, but you could insert "Yes" and "No" or any string or html you desire.
def bool_to_glyph(value)
value ? "<span class='glyphicon glyphicon-ok' aria-hidden='true'></span>".html_safe : "<span class='glyphicon glyphicon-remove' aria-hidden='true'></span>".html_safe"
end
To display the result, invoke in your view as
<%= bool_to_glyph(table_boolean_value) %>
Note: For a simple string display, like "yes" and "no" you do not need to invoke html_safe.
value ? "yes" : "no"
Edited: Helper methods
Helper methods are included under app/helpers/[modelnameplural]_helper.rb
module ExamplesHelper
def bool_to_glyph(value)
value ? "<span class='glyphicon glyphicon-ok' aria-hidden='true'></span>".html_safe : "<span class='glyphicon glyphicon-remove' aria-hidden='true'></span>".html_safe"
end
end
If you want the helper to be available to all controllers, add the method to the application_helper.rb.
Rails and MySQL: Wrong interpretation of boolean values
Figured it out! I didn't restart the application after this change which caused the issue described above.
A fine example of "Have you tried turning it off and on again?"
It was a long day...
How to update boolean value in rails
Please try this to update:
runn = Runn.find_by_id(1)
if !runn.nil?
runn.occupied = true
runn.save
end
Related Topics
Have Pl/Sql Outputs in Real Time
Query for Comma-Separated Ids to Comma-Separated Values
Date Split-Up Based on Fiscal Year
Difference Between "||" Operator and Concat Function in Oracle
Tsql - Use a Derived Select Column in The Where Clause
Update Multiple Records in Multiple Nested Tables in Oracle
Ora-01779: Cannot Modify a Column Which Maps to a Non Key-Preserved Table
How to Create Trigger to Keep Track of Last Changed Data
Sql String Manipulation [Get All Text Left of '(']
Sql Server Freetext Match - How to Sort by Relevance
Good Embedded Database Solution (Like Sqlite) for .Net
How to Treat a Union Query as a Sub Query
Recommended Method to Import a .Csv File into Microsoft SQL Server 2008 R2
Export Data Frame to SQL Server Using Rodbc Package
Create Geometry/Geography Field from Latitude & Longitude Fields (Sql Server)