Hello all, I’m fairly new to rails. I have a view question that
involves, I presume, javascript.
I have two images, one is a LIKE and one is a DISLIKE image.
I want to have one selected by default, the value of which gets saved
to the DB when the user hits the POST button.
Along with selecting a LIKE or DISLIKE (basically selecting true or
false in radio buttons), the user will type a comment in.
So overall we have
- comment: textfield
- like or dislike: boolean or tinyint in the DB: 0 or 1
Selecting either the LIKE or DISLIKE button will not affect the
textfield which remains there for the user’s comment.
I have the radio buttons, the images, and the comment field set up.
I’m not sure how to pre-select one of the images and wire it to
transmit a value.
Any advice in the right direction is greatly appreciated. Thank you.
aj wrote in post #1009746:
I have the radio buttons, the images, and the comment field set up.
I’m not sure how to pre-select one of the images and wire it to
transmit a value.
Any advice in the right direction is greatly appreciated. Thank you.
Option 1:
Pass :checked => true in the options hash.
Option 2 (by example):
xxx.create_people.rb (migration)
class CreatePeople < ActiveRecord::Migration
def self.up
create_table :people do |t|
t.string :first_name
t.string :last_name
t.boolean :super_human, :null => false, :default => false
t.timestamps
end
end
def self.down
drop_table :people
end
end
app/views/people/_form.rb
<%= f.label :super_human %>
<%= f.radio_button(:super_human, "true") %>
<%= f.label :super_human_yes, "Yes" %>
<%= f.radio_button(:super_human, "false") %>
<%= f.label :super_human_no, "No" %>
------------------------------
Rendered HTML:
Super human
Yes
No
------------------------------
- The migration ensures the boolean value in the database is never null
and defaults to false.
- The radio buttons on the form partial represent the value from the
database column so radio button will be checked by default.