Rails: How do I print the request parameters?

后端 未结 8 1625
挽巷
挽巷 2020-12-13 06:11

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

相关标签:
8条回答
  • 2020-12-13 06:19

    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]

    0 讨论(0)
  • 2020-12-13 06:20

    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

    0 讨论(0)
  • 2020-12-13 06:20

    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)
    
    0 讨论(0)
  • 2020-12-13 06:22

    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] %>.

    0 讨论(0)
  • 2020-12-13 06:30

    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.

    0 讨论(0)
  • 2020-12-13 06:32

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

    0 讨论(0)
提交回复
热议问题