Forum: Ruby on Rails pass checked boxes to controller

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C5f924d525edb35300456d2ae343e0e2?d=identicon&s=25 Hunter Walker (siguva)
on 2006-04-24 01:59
Noobie question here.  :)

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!

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
<head>
    Some text here </br>

</head>
<body>
</br>
</br>
</br>


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

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



</body>
</html>
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2006-04-24 02:19
(Received via mailing list)
Hi Hunter,

Hunter Walker 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")
> %></br>
>
> </br>
>   <%= 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
7c4087d053eb02d099a17d91ba5e33b5?d=identicon&s=25 Brian V Hughes (Guest)
on 2006-04-24 02:25
(Received via mailing list)
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
C5f924d525edb35300456d2ae343e0e2?d=identicon&s=25 Hunter Walker (siguva)
on 2006-04-24 02:29
Bill Walton wrote:
> Hi Hunter,
>
> Hunter Walker 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")
>> %></br>
>>
>> </br>
>>   <%= 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.
C5f924d525edb35300456d2ae343e0e2?d=identicon&s=25 Hunter Walker (siguva)
on 2006-04-24 02:30
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!
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2006-04-24 02:32
(Received via mailing list)
I accidently sent the last response prematurely.  Sorry about that.

Hunter Walker 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

<form action="addvisit" method="post">
    #your code
    <input type="submit" value="Save">
</form>

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
C5f924d525edb35300456d2ae343e0e2?d=identicon&s=25 Hunter Walker (siguva)
on 2006-04-24 02:33
Bill Walton wrote:
> I accidently sent the last response prematurely.  Sorry about that.
>
> Hunter Walker 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
>
> <form action="addvisit" method="post">
>     #your code
>     <input type="submit" value="Save">
> </form>
>
> 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!
7c4087d053eb02d099a17d91ba5e33b5?d=identicon&s=25 Brian Hughes (Guest)
on 2006-04-24 02:56
(Received via mailing list)
On Apr 23, 2006, at 08:30 PM, Hunter Walker 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. :) 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
B84d42a3a5c343f8fc6ab7d7f47fd3f5?d=identicon&s=25 Robby Russell (Guest)
on 2006-04-24 02:56
(Received via mailing list)
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. :-)

type: exit to leave breakpointer.

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

-Robby

--
Robby Russell
Founder & Executive Director

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

www.planetargon.com
www.robbyonrails.com

+1 503 445 2457
+1 877 55 ARGON [toll free]
+1 815 642 4968 [fax]
7c4087d053eb02d099a17d91ba5e33b5?d=identicon&s=25 Brian Hughes (Guest)
on 2006-04-24 02:59
(Received via mailing list)
On Apr 23, 2006, at 08:30 PM, Bill Walton 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
>
> <form action="addvisit" method="post">
>    #your code
>    <input type="submit" value="Save">
> </form>

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

> 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") %></br>
Patient #2<%= check_box ("patient", "two", {}, "yes","no") %></br>

   .etc.

Then you can access them in your controller with:

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

-Brian
7c4087d053eb02d099a17d91ba5e33b5?d=identicon&s=25 Brian Hughes (Guest)
on 2006-04-24 03:05
(Received via mailing list)
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. :)

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
B84d42a3a5c343f8fc6ab7d7f47fd3f5?d=identicon&s=25 Robby Russell (Guest)
on 2006-04-24 05:14
(Received via mailing list)
Brian Hughes 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. :)
>
> 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 Russell
Founder & Executive Director

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

www.planetargon.com
www.robbyonrails.com

+1 503 445 2457
+1 877 55 ARGON [toll free]
+1 815 642 4968 [fax]
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2006-04-24 06:21
(Received via mailing list)
Hi Brian,

Brian Hughes wrote:

> On Apr 23, 2006, at 08:30 PM, Bill Walton 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
This topic is locked and can not be replied to.