Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

var_dump and die like php, In ruby on rails (debug in ruby on rails)

This might be repeated question. But I can't display the object.

I'm new to ruby, tried to debug like var_dump and print_r then die in php

Here is my code.

@brand_id = Brand.maximum("brand_id")

I tried the following method

1 puts YAML::dump(@brand_id)
2 logger.debug { @brand_id.inspect }

Can anyone help me resolve it, pls?

like image 905
m2j Avatar asked Jun 29 '15 07:06

m2j


People also ask

How use PHP dump and die?

To dump information about a variable, use the var dump() function. To make the output of the var_dump() method more legible, wrap it with a pre tag. The die() function immediately exits the script. To dump and die, use the var dump() and death() functions together.

What is @user in Ruby?

Controllers use @user to ensure a user object can be shared by different methods (and views) in the controller instance. It may be that during a test a controller instance is initialized and both @user instance variables happen to be created but they are not the same variable.

What is Var_dump in laravel?

This function displays structured information about one or more expressions that includes its type and value. Arrays and objects are explored recursively with values indented to show structure.


2 Answers

Rails will only output views to the browser. Any other output is sent to STD_OUT on the server.

Debugging from views is simple:

<%= debug @brand %>

But debugging from inside a controller or model requires you to either halt the execution with abort, which will output an error page:

abort @brand.inspect

Or you can write to the rails log with:

logger.debug(@brand.inspect)

You can read the log by using tail -f /logs/development.log from the your shell.

like image 86
max Avatar answered Sep 18 '22 12:09

max


The Rails equivalent of php's var_dump would be debug:

<%= debug @brand_id %>
like image 28
zwippie Avatar answered Sep 18 '22 12:09

zwippie