Simple Check Box Question

M

Matt Koidin

I'm new to ruby and rails and I have a simple question that is driving
me crazy. I have a group of check boxes and I want to be able to
default check some of them based on entries in a database.

My display.rhtml looks like this:

<% for category in @categories %>
<dd><%= check_box "category", category.id %><%= category.name %></dd>
<dt>&nbsp;</dt>
<% end %>

If I was writing in HTML I would add a CHECKED option to the checkbox
tag to have it default to checked. How do I do that with ruby?

Thanks,
Matt
 
M

Matt Koidin

I tried that and it doesn't work (again, I might be misinterpreting the
syntax).

If I write:

<%= check_box "category", category.id, true %>

I get an error that says:

undefined method `stringify_keys' for true:TrueClass
 
M

Matt Koidin

I solved the problem --

I switched to using <input type=checkbox> and mimic'd the the hidden
field that the check_box tag gives you.
 
D

Devin Mullins

--------------030304000007080705040702
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Matt said:
I solved the problem --

I switched to using <input type=checkbox> and mimic'd the the hidden
field that the check_box tag gives you.
Mehh.... Evil... A look at the documentation says how to do it:

http://api.rubyonrails.com/classes/ActionView/Helpers/FormHelper.html#M000344

*check_box*(object, method, options = {}, checked_value = "1",
unchecked_value = "0")

Returns a checkbox tag tailored for accessing a specified attribute
(identified by method) on an object assigned to the template (identified
by object). It's intended that method returns an integer and if that
integer is above zero, then the checkbox is checked. Additional options
on the input tag can be passed as a hash with options. The checked_value
defaults to 1 while the default unchecked_value is set to 0 which is
convenient for boolean values. Usually unchecked checkboxes don't post
anything. We work around this problem by adding a hidden value with the
same name as the checkbox.

So your category_id seems to be in the wrong place.

Devin



--------------030304000007080705040702--
 
J

Jeff Waltzer

------=_Part_908_29030685.1119379672154
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

My apologies if this is a excessively newbish question.
Is there way to get one's rails app loaded into fxri such that I can use it=
=20
with Rails to explore what I can do with the different objects of my Rails=
=20
application?

------=_Part_908_29030685.1119379672154--
 
J

Jamis Buck

My apologies if this is a excessively newbish question.
Is there way to get one's rails app loaded into fxri such that I
can use it
with Rails to explore what I can do with the different objects of
my Rails
application?

Not having used fxri before, it should just be a matter of requiring
your config/environment.rb file. Once you do that, you should have
all of your Rails app before you, ready to query.

- Jamis
 
L

Lothar Scholz

Hello Jeff,

JW> My apologies if this is a excessively newbish question.
JW> Is there way to get one's rails app loaded into fxri such that I can use it
JW> with Rails to explore what I can do with the different objects of my Rails
JW> application?

Yes you can use "rdoc --ri <your-rails-app-dir>" and it will generate
the data for the database. But make sure that you don't extend
existing classes like STRING as they are removed.

I work on an easy way to do so with "Arachno Ruby" as the current
ri/rdoc implementation is not the best.
 
H

Hal Fulton

Jeff said:
My apologies if this is a excessively newbish question.

[snip]

I can't answer your question -- hopefully others already have --
but I will comment that newbie questions are welcome here.


Hal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top