Pass checked boxes to controller

Noobie question here. :slight_smile:

I have a form with check boxes on them. I simply want the user to check
the appropriate boxes, click the action (Add Visits). It’s my
understanding, that these check box values go into a hash
(add_visit_for_this_Pt), but I can’t seem to get this hash back to my
controller for processing. Below is my form.

Basically, all the controller is doing/going to do is call an api and
send some data.

Thank you very much for your help!

Some text here

Patient #1<%= check_box (“add_visit_for_this_Pt”, “1”, {}, “yes”,“no”)
%>
Patient #2<%= check_box (“add_visit_for_this_Pt”, “2”, {}, “yes”,“no”)
%>
Patient #4<%= check_box (“add_visit_for_this_Pt”, “4”, {}, “yes”,“no”)
%>
Patient #8<%= check_box (“add_visit_for_this_Pt”, “8”, {}, “yes”,“no”)
%>
Patient #9<%= check_box (“add_visit_for_this_Pt”, “9”, {}, “yes”,“no”)
%>

<%= link_to "Add Visits", :action => "addvisit" %> #This is the method in my controller

Hi Hunter,

Hunter W. wrote:

I have a form with check boxes on them.
I simply want the user to check the appropriate
boxes, click the action (Add Visits). It’s my
understanding, that these check box values go into
a hash (add_visit_for_this_Pt), but I can’t seem to
get this hash back to my controller for processing.

The values get passed to the controller automatically when the form is
submitted. No extra work required on your part.

Patient #9<%= check_box (“add_visit_for_this_Pt”, “9”, {}, “yes”,“no”)
%>

<%= link_to "Add Visits", :action => "addvisit" %> #This is the method in my controller

In your addvisit action method, you’ll access the values passed back
with
something like

All form data is passed to the controller as part of the params
object. If you are unsure as to the format of the params object, what
you can do is place:

render :text => params.debug

As the last line of your controller. This will send back the value of
the params object to the browser. Quick side note: having recently
been bitten by the TDD Bug :), this is probably the worst way to do
this kind of testing. Without writing your tests as repeatable
methods you’re just flying by the seat of your pants. I know, this is
how I’ve “tested” just about every web app I built in the past 10
years! I strongly recommend, to everyone still testing like this, to
learn about Rails’ version of Unit and Functional tests. They are
easy to write, easy to execute and, most importantly, repeatable…

-Brian

Brian V Hughes wrote:

All form data is passed to the controller as part of the params
object. If you are unsure as to the format of the params object, what
you can do is place:

render :text => params.debug

As the last line of your controller. This will send back the value of
the params object to the browser. Quick side note: having recently
been bitten by the TDD Bug :), this is probably the worst way to do
this kind of testing. Without writing your tests as repeatable
methods you’re just flying by the seat of your pants. I know, this is
how I’ve “tested” just about every web app I built in the past 10
years! I strongly recommend, to everyone still testing like this, to
learn about Rails’ version of Unit and Functional tests. They are
easy to write, easy to execute and, most importantly, repeatable…

-Brian

I’ve heard that I should be doing this and I will take your advice and
start. I have to admit, though, my first rails app is a bit hacky and I
am just trying to get ahold of the technology.

Thanks you!

Bill W. wrote:

Hi Hunter,

Hunter W. wrote:

I have a form with check boxes on them.
I simply want the user to check the appropriate
boxes, click the action (Add Visits). It’s my
understanding, that these check box values go into
a hash (add_visit_for_this_Pt), but I can’t seem to
get this hash back to my controller for processing.

The values get passed to the controller automatically when the form is
submitted. No extra work required on your part.

Patient #9<%= check_box (“add_visit_for_this_Pt”, “9”, {}, “yes”,“no”)
%>

<%= link_to "Add Visits", :action => "addvisit" %> #This is the method in my controller

In your addvisit action method, you’ll access the values passed back
with
something like

Thank you for your reply, Bill. I think the last part of your post was
cut-off however.

I accidently sent the last response prematurely. Sorry about that.

Hunter W. wrote:

I have a form with check boxes on them.

Actually, in Rails terms, you don’t have a form. To make a form, you’ll
have to wrap what you’ve got below with

#your code

When the user clicks the “Save” button, Rails will pass the values back
to
the contoller in the params hash. Then, in your addvisit action you’ll
be
able to access the values with code like

if params[:add_visit_for_this_PT][:1] == “yes”

do whatever

end

You’ll probably want to check out the documentation on
ActionView::Helpers::FormHelper at http://api.rubyonrails.com/

HTH,
Bill

Bill W. wrote:

I accidently sent the last response prematurely. Sorry about that.

Hunter W. wrote:

I have a form with check boxes on them.

Actually, in Rails terms, you don’t have a form. To make a form, you’ll
have to wrap what you’ve got below with

#your code

When the user clicks the “Save” button, Rails will pass the values back
to
the contoller in the params hash. Then, in your addvisit action you’ll
be
able to access the values with code like

if params[:add_visit_for_this_PT][:1] == “yes”

do whatever

end

You’ll probably want to check out the documentation on
ActionView::Helpers::FormHelper at http://api.rubyonrails.com/

HTH,
Bill

Excellent! Thank you, Bill!

On Apr 23, 2006, at 08:30 PM, Hunter W. wrote:

I’ve heard that I should be doing this and I will take your advice and
start. I have to admit, though, my first rails app is a bit hacky
and I
am just trying to get ahold of the technology.

Of course. That’s why I posted the Q&D way of seeing the data in the
params object. You’ll need to get familiar with how data lives in
there, since it’s one of the major driving forces behind most Rails
applications.

Also, the note about repeatable testing wasn’t just for you. :slight_smile: I see
posts to this list, almost daily, from people doing things the way I
used to do them. I’m far from being a TDD expert, but the little that
I’ve done with it has been a real eye opener.

Once I can build a fully functional Rails app where I never open the
browser until I need to debug the CSS, I figure I’ll be fully
converted to the TDD-way!

-Brian

On Apr 23, 2006, at 08:30 PM, Bill W. wrote:

Actually, in Rails terms, you don’t have a form. To make a form,
you’ll have to wrap what you’ve got below with

#your code

This is a good point. I was so focused on not looking at the HTML
that I didn’t catch this first time through. :slight_smile:

When the user clicks the “Save” button, Rails will pass the values
back to the contoller in the params hash. Then, in your addvisit
action you’ll be able to access the values with code like

if params[:add_visit_for_this_PT][:1] == “yes”

do whatever

end

Actually, you can’t do this. Ruby symbols can’t start with numbers.
But, you can use string-ified numbers as keys to a Ruby hash. So:
params[:add_visit_for_this_PT][‘1’] is fine. Since I’m a huge fan of
using symbols every place I can, I would probably build my checkboxes
to look more like this:

Patient #1<%= check_box (“patient”, “one”, {}, “yes”,“no”) %>
Patient #2<%= check_box (“patient”, “two”, {}, “yes”,“no”) %>

.etc.

Then you can access them in your controller with:

params[:patient][:one]
params[;patient][:two]

-Brian

Brian V Hughes wrote:

of testing.
If you’re getting bitten by the TDD bug… you might consider playing
with breakpointer when you get to this point.

For example, if your controller looked like this:

class MyController < ApplicationController
def index
# renders your form…which submits to the other action
end

def foobar
breakpoint <======
# does stuff… and renders something?
end
end

Then when you submit your form it will hit the breakpoint and wait for a
breakpointer session to intervene.

In another terminal… from the root of your Rails application run:

ruby script/breakpointer

It’ll attempt to make a connection to a breakpoint instance… and when
your form submission occurs it’ll pause the application in its current
state. Look in the terminal where breakpointer is running and you
should see that its now in irb and ready for you to check out your
application.

type in: params… you should see everything within the params there…
along with a bunch of other stuff. :slight_smile:

type: exit to leave breakpointer.

This is much nicer than inspecting stuff in your browser. :slight_smile:

-Robby


Robby R.
Founder & Executive Director

PLANET ARGON, LLC
Ruby on Rails Development, Consulting & Hosting

www.robbyonrails.com

+1 503 445 2457
+1 877 55 ARGON [toll free]
+1 815 642 4968 [fax]

Brian H. wrote:

Sweet! I’ll definitely give that a try. I’ve always got 2 terminal
windows open to my apps root directory when I’m developing. I used to
use the second one for the script/server instance, so I could watch the
log file lines go by, but I like the idea of replacing that with
script/breakpointer a whole lot more. :slight_smile:

Can you just leave breakpointer running? Does it poll the app, to see if
a breakpoint session has been created? Or do you need to re-launch
breakpointer each time?

You can leave it running. breakpointer is essentially a client script
that keeps trying to connect to an instance of a breakpoint service,
which is what is instantiated when the application executes breakpoint.

When you exit, it just ends the session and then attempts to connect to
a new instance of breakpoint.

You’ll notice that if you put a breakpoint in a controller and load the
action in your browser that the page will not load… but when you exit
the breakpoint… it’ll resume and finish loading the page.

It’s quite fun!

Robby


Robby R.
Founder & Executive Director

PLANET ARGON, LLC
Ruby on Rails Development, Consulting & Hosting

www.robbyonrails.com

+1 503 445 2457
+1 877 55 ARGON [toll free]
+1 815 642 4968 [fax]

Sweet! I’ll definitely give that a try. I’ve always got 2 terminal
windows open to my apps root directory when I’m developing. I used to
use the second one for the script/server instance, so I could watch
the log file lines go by, but I like the idea of replacing that with
script/breakpointer a whole lot more. :slight_smile:

Can you just leave breakpointer running? Does it poll the app, to see
if a breakpoint session has been created? Or do you need to re-launch
breakpointer each time?

-Brian

Hi Brian,

Brian H. wrote:

On Apr 23, 2006, at 08:30 PM, Bill W. wrote:

if params[:add_visit_for_this_PT][:1] == “yes”

do whatever

end

Actually, you can’t do this. Ruby symbols can’t start with numbers.

Great catch! Thanks. I knew that looked wrong ;-p

Best regards,
Bill