inspect array in controller rails and ruby - ruby-on-rails

I want to inspect my array with all relation (belognTo and hasMany) in ruby and rail without loop in controller file. is there any way such like a php? I have try this
abort(#microposts.inspect)
but error is coming. Who can do this ?

just use
logger.debug "microposts---------->#{#microposts.inspect}"
The above line will print the micropost object in your console log
I suggest that you check Ruby on Rails Debugging Documentation.

Related

How to get all files in image directory Rails4 in array

I have migrated from Rails3 to Rails4. The following code returns Array in Rails3 but in Rails4 it returns string with illegal character.
Dir.glob("app/assets/images/flowers/*")
sample output in Rails3
["app/assets/images/flowers/rose.png", "app/assets/images/flowers/lilly.png"]
output in Rails4
"\x04\b[dI\"8app/assets/images/flowers/rose.png\x06:\x06ETI\"4app/assets/images/flowers/lilly.png"
How to get same output format as in Rails3?
try this
files = Dir.glob("app/assets/images/flowers/*").map do |f| File.basename f end
Dir has nothing to do with Rails — it's pure Ruby class. Here is the API reference to it. According to API it should always return an Array. My guess is that you messed up something in your Ruby installation while you were upgrading Rails 3 to 4.
I think best bet will be a clean installation of ruby/rails. You could also try to run Dir.glob() from both IRB and rails console to see where the mistake happens; and start from there.

How to get the value of a Controller variable in rails console

I set a variable inside a Controller and I'm trying to just do something as simple as read that variable in the rails console.
I thought about just doing #test in the console which is the name of the variable. but it shows as >null. When I do puts under where I set the variable it traces out the correct value in my terminal window.
Any ideas what I need to do to get to this variable via the console.
I tried putting the name of the controller first and then .variable but that threw an error
I can see what's inside my models by just using the model name and some attributes like .first and .last
You'd have to instantiate the controller and provide a public accessor to get the value in rails console.
If you're trying to debug something, I recommend you check out Pry. It's a Ruby debugging REPL. Do a require 'pry' in your controller, and put binding.pry somewhere in an action, when you execute that controller method--either interactively in a browser, or via a functional test (I recommend the latter)--it will open the Pry REPL and #test will be in scope there.
Check out this Railscast for some help using it.
Alternately, just rely on good unit or functional testing. Write a test around the method and add an assertion on assigns(:#test) to compare the value to your expectation. Check out the RSpec controller spec documentation.

"puts" method in rails 3

I've been doing some development with rails 3 and I was wondering why calling the "puts" method doesn't output to standard out.
How do I get this back?
Instead of puts, use logger.info:
logger.info #collection.inspect
You can also use the standard ruby way by calling STDOUT << 'your output'. Method put is not a rails speciality, it comes with ruby. If you use rails, you can also rely on the logger object.

Why can't I output a path when in rails console?

I tried doing this when in rails console:
puts about_home_index_path
It gave me an erorr, why is that? should it work?
It was answered here: Rails: how do you access RESTful helpers?
You should use:
puts app.about_home_index_path

how to print varient value on rail3 controller

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.

Resources