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.
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.
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…
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.
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.
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/
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/
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!
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.
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:
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 R.
Founder & Executive Director
PLANET ARGON, LLC
Ruby on Rails Development, Consulting & Hosting
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 R.
Founder & Executive Director
PLANET ARGON, LLC
Ruby on Rails Development, Consulting & Hosting
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?