Newbie: accessing form parameters in controller action?

Newbie alert!

I am saving a form, and in my save action in the controller, I can do

logger.info(params)

and see a string in the log like

“commitSaveactionsavecontrollerfoofooattrib1value1attrib2value2…”

which is all of the info from my form, controller (foo), action (save),
model (foo), and attribute values. It’s all there, but how do I get at
it?

I would like to get at the attribute values (form fields) discretely,
and thought I could do something like

logger.info(params[:attrib1])

which might return

value1

But it doesn’t seem to return anything. Several other newbie attempts
to query params fail too. What gives?

Sorry to be a dunce.

Tom

That should work ok. Try this to inspect them easier in your logs:

logger.debug params.inspect

That should help you I think.

Thanks William.

So in a non-contrived case I logger.debug params.inspect returns

{“commit”=>“Add Comment”, “action”=>“save_comment”,
“controller”=>“survey”, “survey”=>{“new_comment_body”=>“My Comment”,
“survey_id”=>“1”, “new_comment_title”=>“My Title”}}

None of these variants, starting one line later in the same method
produces anything in the log (perhaps a newline?)

logger.debug params[:new_comment_title]
logger.debug "#{params[:new_comment_title]}"
logger.debug "#{params['new_comment_title']}"

I am missing something stupid … but what?

Thanks –

Dunce Boy Tom

Tom H.on wrote:

So in a non-contrived case I logger.debug params.inspect returns

{“commit”=>“Add Comment”, “action”=>“save_comment”,
“controller”=>“survey”, “survey”=>{“new_comment_body”=>“My Comment”,
“survey_id”=>“1”, “new_comment_title”=>“My Title”}}

new_comment_title is inside the survery hash:
params[:survey][:new_comment_title]

Fred

Here is the deal, the key “new_comment_title” is actually in the hash
pointed to by “survey” so you would access it like this:

logger.debug params[:survey][:new_comment_title]

William P. wrote:

Here is the deal, the key “new_comment_title” is actually in the hash
pointed to by “survey” so you would access it like this:

logger.debug params[:survey][:new_comment_title]

William & Fred –

Aha! (Doh!)

Thanks. No, really, thanks. My wife thanks you, too, since I was
obsessively trying different things that didn’t work.

Tom

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs