Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Provide params hash for put / post requests in rails console

I find it more convenient to check response for some requests from within console

>> app.put '/users/2/' => 500 

But wasn't able to find a way to specify request parameters. How I have to do that?

like image 276
jibiel Avatar asked Dec 12 '11 12:12

jibiel


People also ask

What is params hash in rails?

Specifically, params refers to the parameters being passed to the controller via a GET or POST request. In a GET request, params get passed to the controller from the URL in the user's browser.

Is params a hash?

While params appears to be a hash, it is actually an instance of the ActionController::Parameters class.

What is params ID in rails?

params[:id] is meant to be the string that uniquely identifies a (RESTful) resource within your Rails application. It is found in the URL after the resource's name.

How do I get rails console?

Go to your browser and open http://localhost:3000, you will see a basic Rails app running. You can also use the alias "s" to start the server: bin/rails s . The server can be run on a different port using the -p option. The default development environment can be changed using -e .


2 Answers

If you want to put or post to a URL there are also methods for that. You can copy/paste the parameters exactly as they are displayed in your Rails production log:

app.post('/foo', {"this" => "that", "items" => ["bar", "baz"]}) app.put('/foo', {"this" => "that", "items" => ["bar", "baz"]}) 

If you want to sent a custom header, you can add an optional third parameter:

app.post('/foo', {:this => "that", :items => ["bar", "baz"]}, {"X-Do-Something" => "yes"}) 

Any of the get/post/put/delete methods will display their full log output on the console for you to examine. If you want to get information such as the response body returned, HTTP status or response headers these are easy too:

app.response.body  app.response.status  app.response.headers.inspect 

Source: http://andyjeffries.co.uk/articles/debug-level-logging-for-a-single-rails-production-request

like image 138
Nakul Avatar answered Sep 22 '22 13:09

Nakul


The above has changed to

app.post '/foo', params: {"this" => "that", "items" => ["bar", "baz"]} 

Also for forms I had to give an authenticity_token as well. So in my example the full command was

app.post '/login', params: {email: '[email protected]', password: 'abcd', authenticity_token: 'my_authenticity_token_generated_for_this_view' } 
like image 36
Jayanti Hari Avatar answered Sep 18 '22 13:09

Jayanti Hari