Tutorial :in Ruby on Rails 2.3.2, how to print out params during a create action?



Question:

there is a scaffold created Story... and in the create action, there is

@story = Story.new(params[:story])  

i was curious as to what is in params... so i want to dump out params... but there is no view associated with the create action... is there a way to dump out its content? is there a way to dump out the POST variables in of my code too? (to see what's going on in the lower level)


Solution:1

The easiest thing to do is just dump params out to the log:

Rails.logger.info("PARAMS: #{params.inspect}")  

If you're in development mode, just look in your development.log and that line will be there.

The params scope is a combination of URL/FORM (GET/POST) fields, and it will be printed out in the log as part of the normal output processing, so you might not need your own dumping of it - any development or production log contains the params dump at the top of the log line, e.g.

Processing Clients::ClientsController#show (for x.x.x. at 2009-05-24 00:34:26) [GET]    Parameters: {"id"=>"303", "user_id"=>"2"}  


Solution:2

Now I know Rails more, you can also simply use a

p params  

in your code and look at the console's output (the log shown on the console)


Solution:3

If you're on a Mac, Spike is a great little app the analyses log files and will let you inspect params for requests, amongst other things.


Solution:4

Using Fiddler on Windows, it is shown

the HTTP line #1 is:

POST /stories HTTP/1.1  

this is the POST content:

authenticity_token=62iw%2BrsxlCFsbnxsS7FXKRn6CcvJfjottrsBPlM5lZo%3D&story%5Bname%5D=Google+Main+Site&story%5Blink%5D=www.google.com&commit=Create

listed in a table:

authenticity_token  62iw+rsxlCFsbnxsS7FXKRn6CcvJfjottrsBPlM5lZo=  story[name]         Google Main Site  story[link]         www.google.com  commit              Create  

and the server log is:

Parameters: {"commit"=>"Create", "story"=>{"name"=>"Google Main Site", "link"=>"www.google.com"}, "authenticity_token"=>"62iw+rsxlCFsbnxsS7FXKRn6CcvJfjottrsBPlM5lZo="}


Solution:5

You don't need to anything except look in your logs (they live in /log). Unless you're fiddling with something, the logging of parameters is turned on by default in all logs.

Processing PostsController#create (for 127.0.0.1 at 2009-05-24 13:03:24) [POST]    Parameters: {"commit"=>"Create", "authenticity_token"=>"2G6BKOs8xNAaXiToVf4r1ko8QZzP9QAomi2PHVQC5Oc=", "story"=>{"something"=>"asdfafd"}}  

Parameters lists all parameters, and the hash following "story" is the equivalent of params[:story] (everything comes to the server as strings, and Rails turns it into a HashWithIndifferentAccess so that you can access it with a symbol).


Solution:6

If you're on a *NIX system (including OS X) open a new terminal window/tab and type the following command:

tail -f log/development.log

You'll get a constant stream of requests coming in -- including params -- and the resulting DB actions. Invaluable for development/debugging, IMO.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »