How can I make a check box default to being checked in Rails 1.2.3?
If you're using check_box
in the context of a form, then the check box will show whatever value that field has.
@user = Person.find(:first)
@user.active = true
check_box 'user', 'active' #=> will be checked by default
If you're using check_box_tag
, the third parameter is the initial state (check_box_tag
doc):
check_box_tag "active", 1, true
How to make a check_box checked in rails?
Here's how to do it as of rails 4, I didn't check older documentation.
<%= check_box("tag", tag.id, {checked: true}) %>
This will make the checkbox checked. Of course instead of true
you will put in some logic which determines if each one is checked.
Display Rails check box to be either checked or unchecked based on the value in the database
the third value is the checked.
So,
<%= check_box_tag 'done', '1', item.done %>
1 is the value the third item is the checked value
RoR: Semantic-ui and check box
I found a solution; all I have to do is to change the order of
f.check_box and f.label
This one is working:
<div class="ui checkbox">
<%= f.check_box :remember_me %>
<%= f.label :remember_me, "Angemeldet bleiben?" %>
</div>
Rails 1.2.3 Unserializing data
I think the problem is a matter of scope; the checked
variable only exists in one of the branches of the if
and does not exist once the end
is reached. Try this:
<p><label for="supplemental_material_type_exclusions">Exclusions</label><br/>
<%= ApplicantBudgetlevel.find(:all).collect { |p|
checked = false
if @supplemental_material_type.exclusions
checked = @supplemental_material_type.exclusions.include?(p.level)
end
"#{check_box_tag('supplemental_material_type[exclusions][]', p.level, checked)} #{p.level} - #{p.range}<br />\n"
} %>
</p>
I simply removed the else
branch and set the value to false
by default.
This might cause further failures if the check box should only be shown if the if
branch is true -- in which case, try the following:
<p><label for="supplemental_material_type_exclusions">Exclusions</label><br/>
<%= ApplicantBudgetlevel.find(:all).collect { |p|
if @supplemental_material_type.exclusions
checked = @supplemental_material_type.exclusions.include?(p.level)
"#{check_box_tag('supplemental_material_type[exclusions][]', p.level, checked)} #{p.level} - #{p.range}<br />\n"
end
} %>
</p>
Which of the two approaches is correct depends upon more than I can gauge from here -- but I'd try the first one first, and look through the logs for new or different errors or warnings.
rails check_box_tag disapear
Wrap the if
logic inside parenthesis.
<%= check_box_tag 'clans[]','Feniks', (true if !@clans.nil? and @clans.include? 'Feniks') %>
In your code, if
working as a modifier. Like print 10 if x.present?
. In this sample, if x
is not blank, the this method call print(10)
will be happened, otherwise not. Same analogy holds for your case also. Inside the erb tag, you code is being interpreted as :
<%= check_box_tag('clans[]','Feniks', true) if <condition> %>
That's why when condition is true, check_box_tag
method is getting invoked otherwise not. You could write the whole if
part as :
<%= check_box_tag 'clans[]','Feniks', @clans.try(:include?, 'Feniks') %>
Related Topics
Connecting to Oracle Db Using Ruby
Ruby on Rails 4 - What Authentication Gem to Use
How to Install a Gem Globally Without Sudo Using Rbenv
Error Installing Gem: Couldn't Reserve Space for Cygwin's Heap, Win32 Error 487
Read Contents of a Local File into a Variable in Rails
Is Assignment in a Conditional Clause Good Ruby Style
How to Get a Substring from Position N to the Last Char in Ruby
Is There a Ruby One-Line "Return If X"
Machinist VS Factorygirl - Pros and Cons
How to Select the Longest String from a Ruby Array
Marking an Unused Block Variable
Programmatically Getting Full Ruby Version
Devise Authentication Gem: How to Save the Logged in User Id
Start Using Ruby on Rails, Web Services and Oauth
How to Implement Options Hashes in Ruby