Rails: How do I print the request parameters?

Ruby on-Rails

Ruby on-Rails Problem Overview


I have a Rails 3 application and I want to print in the view the request parameters. How do I do it?

Edit:

The purpose is to see what is being sent in a Form.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

If you wanted to print all of the parameters, the easiest way would be to use the inspect

puts params.inspect

or better, use the Rails logger

Rails.logger.debug params.inspect

In your html/ERB, you can use

<%= params.inspect %>

Solution 2 - Ruby on-Rails

I would use debug(params). That will give you a nicely formatted view of them.

Solution 3 - Ruby on-Rails

Learnt this from the Ruby Hero James Edward Gray II on this episode of Ruby Rogues podcast which I highly recommend. raise is a swiss army knife for inspecting anything in your Rails code which prints it nicely on your browser.

> raise params.inspect

Solution 4 - Ruby on-Rails

Parameters are stored in the params hash. For example, if there was a title parameter, you could display it in your view using <%= params[:title] %>.

Solution 5 - Ruby on-Rails

pretty_print also can be useful, in ruby and rails apps both

pp params

pretty_print doc: http://ruby-doc.org/stdlib-2.1.0/libdoc/pp/rdoc/PP.html

Solution 6 - Ruby on-Rails

Something like

<%= params.inspect %>

works, but what I would like to add is the following gem and Chrome plugin which was literally an eye-opener.

I am putting it here because I think it will help people check out params hashes, see SQL queries or view Errors.

Solution 7 - Ruby on-Rails

You can use for models, controllers, etc.

puts YAML::dump(params)

Source: Ruby / Rails alternative to PHP print_r() and var_dump()

For views:

DebugHelper’s debug(object)

In your case:

DebugHelper’s debug(params)

Solution 8 - Ruby on-Rails

Source: http://guides.rubyonrails.org/getting_started.html#creating-articles > When a form is submitted, the fields of the form are sent to Rails as parameters. These parameters can then be referenced inside the controller actions, typically to perform a particular task. To see what these parameters look like, change the create action to this:

def create
  render plain: params[:article].inspect
end

The response when POST'ing a form to the targeted #create route would be a plain-text hash output of params[:article]

Solution 9 - Ruby on-Rails

ap params.to_unsafe_h

Example output:

[19] pry(#<AssessmentsController>)> ap params.to_unsafe_h
{
                  "utf8" => "✓",
    "authenticity_token" => "",
            "assessment" => {
        "form_field_10" => "0",
        "form_field_12" => "1"
    },
                "commit" => "Create Assessment",
            "controller" => "assessments",
                "action" => "create"
}
=> nil

Uses https://github.com/awesome-print/awesome_print

Based on https://stackoverflow.com/a/49342607

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNerianView Question on Stackoverflow
Solution 1 - Ruby on-RailsRob Di MarcoView Answer on Stackoverflow
Solution 2 - Ruby on-RailsAndrewView Answer on Stackoverflow
Solution 3 - Ruby on-RailsKibet YegonView Answer on Stackoverflow
Solution 4 - Ruby on-RailsDylan MarkowView Answer on Stackoverflow
Solution 5 - Ruby on-RailsitsnikolayView Answer on Stackoverflow
Solution 6 - Ruby on-RailsalextsView Answer on Stackoverflow
Solution 7 - Ruby on-Railsd.danailovView Answer on Stackoverflow
Solution 8 - Ruby on-Railswes.hysellView Answer on Stackoverflow
Solution 9 - Ruby on-RailsbunufiView Answer on Stackoverflow