i want to print some variant value on console or logs then i can check is there something wrong,
i tried puts & print ,i didn't find output info,how to solve this,thanks,i'm new on rails
puts and print will output code in the console for code run in the console. A better method usually is to use the built in logging. For logging that you only want recorded in development, you can use the "logger.debug()" method. Just pass in a string as an argument and look for the results in log/development.log
logger.debug("Time.now is #{Time.now}")
Something like that.
Related
I have a Rails method that ends like this:
puts encrypted
return encrypted
Console outputs:
#?$???z???e7Bw?1I?F???????s?w
=> "#\x9A$\xB1\xBA\xF4z\x8F\x97\xECe\a7Bw\xE01I\xEDF\xA6\xBE\xEA\xE0\xFC\xF6\xB9\x1Cs\x00\xC0w\x14"
Why do these results look different when output in the same place and without any encode/decode instructions?
How can I get Rails to output the longer version when I call puts encrypted?
IRB calls inspect on its values before dumping them in order to expose codes that might get output as just ? or worse, in this kind of situations.
Try p encrypted, then try puts encrypted.inspect.
I'm using system(" ") on the rails console to execute an application that takes inside a file, processes it and outputs another file.
The application also logs statistics for said file on the terminal before closing.
My objective is to save those logged statistics as variables inside rails, although I'm not exactly sure how I can do that.
How can I save the logged statistics inside rails variables?
I don't think the system method is the best method to use here, as I think this would either return true or false. However to get back the value, you should try to use the backticks
statistics = `method to call on computer`
let me know if that works
I want to get some lines printed in irb opened through rails console. I have seen a lot of SO questions of how to achieve it. But I get nothing in the irb.
below is the code--
def show
puts 'in show method'
#post = Feed.find_by_id params[:id]
puts #post.inspect
redirect_to root_path unless #post.present?
end
now I have opened server by command rails server. Also, In another terminal I gave the command rails console, it opened the irb prompt. when in browser I run localhost:3000/posts/82 it gives the correct post, but nothing is shown in the console. What step am I missing? I want to print something in the console when a particular method is called.
Best way to debug is to use the debugger command.
If you are using ruby 2.0 or above, you have to use the gem 'byebug' and if you are using 1.9 or below, then gem ruby-debug
then, when you run your server in development mode, your server will stop when it reaches the debugger allowing you to see your objects' state and modify them (much better than simply using puts
The program will stop in the same window that your server runs.
Some basic commands:
c continues the execution until next debugger is found
n runs the next command. If it is a function executes the
function
s step into the next command. If it is a
function, you will get into the function and see the variables
display expression on every step display the result of the
expression you write (very useful when debugging loops)
undisplay expression_number stops displaying the expresion
display shows all the expressions being displayed
list Displays the source code being executed
help shows the available commands help
command_name shows detailed info about a command
More info about debugging: http://guides.rubyonrails.org/debugging_rails_applications.html
The puts 'in show method' in line 2 won't show the output in rails console. Instead it shows the output in the same terminal where you did rails server. They might be lost with so much of output, so try to find it there itself.
Use Rails.logger.debug "in show method" etc.
In the second tab in terminal tail log/development.log like this
$ cd rails_app_root
$ tail -f log/development.log
or
$ cd rails_app_root
$ less +F log/development.log
There you will find all the output from the console.
Try to use Rails.logger
Rails.logger.info "Some debugging info I want to see in my
development log.------#{#post.inspect}"
It will print #post value in log file.
I am a big fan of puts debugging. e.g;
def index
method = Kernel.instance_method(:method)
p method.bind(request).call(:headers).source_location
#users = User.all
end
The above snippet helps you find where the method is implemented:
Processing by UsersController#index as */*
["/Users/aaron/git/rails/actionpack/lib/action_dispatch/http/request.rb", 201]
You can find more cool puts debugging here.
I am having problems with the Rails runner. When I try to use if, even by command line, it does nothing! it doesn't show error messages, nor results from actions.
For example, if I try
rails runner Credit.count
having defined the model Credit, and the method count as
Credit.first.update_attribute(:estado, "En proceso")
or even simpler tasks, the runner does nothing!
I've tried saying that the environment is development, but nothing works. Does anyone has any insights? Am I doing something wrong?
Edit: I am watching the database, that's the problem, there is no update. I changed the value of the column "estado" for the first element to something other than "En proceso", however, no matter how many times I use the runner, the db doesn't show any change at all.
rails runner: runner runs Ruby code in the context of Rails non-interactively
update_attribute : it only return true or false, if you want to output the result, you can use "puts" or "p".
For example,
$ rails r "User.first" #no output, even it will return a user object
$ rails r "puts User.first" # you can use "puts" get the output
#<User:0x007f8a2c76e608>
if you just written Credit.first.update_attribute(:estado, "En proceso") in console, you shouldn't have results, but you could see result in DB, for checking if update_attribute really update it you could write as update_attribute! also you could just write:
p Credit.first.update_attribute(:estado, "En proceso") which should print true or false to console
Turns out I am an idiot.
First, I had to change the name of the method, calling Credit.count calls the default count method, so I changed it to myMethod.
After that, I was able to use puts function and update the database easily :)
What is the best way for me to determine a controller variable's value during execution?
For example, is there a way I can insert a break in the code, and cause the value of the variable to be output to the screen (or the log)?
Yes. The easiest way is to raise the value as a string. Like so: raise #foo.to_s
Or, you can install the debugger (gem install ruby-debug), and then start the development server with the --debugger flag. Then, in your code, call the debugger instruction.
Inside the debugger prompt, you have many commands, including p to print the value of a variable.
Update: here's a bit more about ruby-debug.
If you have a controller instance variable named #foo, then in your controller you can simply do something like:
logger.debug "#foo is: #{#foo}"
Additionally, you can output the value in your view template using:
<%= debug #foo %>
I prefer using the inspect method like so:
raise #foo.inspect
It has more information than to_s, like the attribute values.
Summary from Jordi Bunster, John Topley, and Jaryl:
I. Quick and dirty way:
raise #foo.inspect
in your controller. Or
<% raise #foo.inspect %>
in your view.
II. Proper logging to you development.log:
logger.debug "#foo == #{#foo.inspect}"
III. Full-fledged debugging:
Install the debugger (gem install ruby-debug), and then start the development server with the --debugger flag. Then, in your code, call the debugger instruction.
Inside the debugger prompt, you have many commands, including p to print the value of a variable.
Raising an exception is the fastest way if you just need to look at a value, but it's worth the time to learn how to use the debugger properly. It's rare that you would only need to just see the value of a variable, you are likely trying to find a bug in your code, and that's what a debugger is for.
Sending the info to the development log is slower than either of the other two options here so far if you learn how to use the debugger (who wants to read through log files). Use the logger for production, you are going to want to see what the value was when somebody calls you up and says everything is broken.
Well, I usually prefer the standard error output
$stderr.print("whatever")
Its simple and does the job.
Add pry-moves to Gemfile: gem 'pry-moves'
Insert binding.pry where you want to stop
Type variable's name to see its value
Then continue by typing c, move to next line with n or perform other debugging actions until you will resolve the issue.