Controller access to POST Data in bulk?

Hi,

Ruby N. here, with a question. I’ve written rails web app, and I’d
like to have the controller log the post data into the info log to keep
track of how users are using my app. Is there a way to reference all of
the POST data in bulk, or will I have to call out each field separately?
The form has a lot of fields, so maintaining the logging feature will be
a pain if it can’t be done in one wack, by referencing a certain hash or
something.

Any advice greatly appreciated.

scafativ

Vc Scafati wrote:

a pain if it can’t be done in one wack, by referencing a certain hash or
something.

Any advice greatly appreciated.

What about the params hash?

7stud – wrote:

What about the params hash?

And the standard library module yaml can format the output for you:

require ‘yaml’

params = {:name => “Ted”, :age => 32, :email => “[email protected]”}
puts YAML.dump(params)

–output:–

:age: 32
:email: [email protected]
:name: Ted

ActionController::Request#body

http://api.rubyonrails.org/classes/ActionController/Request.html#M000701

dwh

Vc Scafati wrote:

Hi,

Ruby N. here, with a question. I’ve written rails web app, and I’d
like to have the controller log the post data into the info log to keep
track of how users are using my app. Is there a way to reference all of
the POST data in bulk, or will I have to call out each field separately?

Do you mean write something like:

puts params[:name]
puts params[:age]
puts params[:email]

???

Like most programming languages, ruby has loops, so you could always
write:

params = {:name => “Ted”, :age => 32, :email => “[email protected]”}

params.each do |key, val|
puts “#{key} = #{val}”
end

–output:–
name = Ted
age = 32
email = [email protected]

That would allow you to format the data anyway you want.

One advantage of using yaml is that you can later use a method in
another program to reload the data back into a hash to analyze the data,
i.e. output all the users by age category or by location. yaml has the
advantage that it is both human readable and can be reloaded into a
hash. You can even edit the yaml file by hand.

Similar to yaml is marshall–except marshal is built into ruby and it is
faster. However, the files marshal generates are not human readable and
not editable by hand. Marshal is used to store ruby data in a program
that is stored in hash, array, or object of a class, and then reload the
object at a later date for use in another program.