Using rails console - ruby-on-rails

I am working with the Salesforce Ruby API which has a pretty good, but slightly dated set up guide. After installing the right gems it says to enter 'script/console' into the command line, and then enter '>> Salesforce::Contact.first' which should respond with a user id.
I have read other's post saying that script/console is no longer being used and to use rails console instead. That works fine, but when I enter '>> Salesforce::Contact.first' it complains that the syntax is wrong.
I also tried removing the >> from the front and it resulted in it saying "The filename, directory name, or volume label syntax is incorrect"
I there some other command besides '>> Salesforce::Contact.first' that I should be using?
Thanks

>> (and similar things) represent the prompt that you should see already on the screen for you. It's essentially the console saying "Go ahead, type something"
Try to 'mentally' strip stuff like that off from the beginning, including spaces. This would mean that the 'command' you're looking to execute is really,
Salesforce::Contact.first
Hope this helps.

Related

How to use Nokogiri to scrape data on a cloud development environment (Cloud9)

I'm currently using Cloud9 as a cloud-based IDE, but unfortunately it seems like running things on Cloud9 adds a layer of complexity that I don't quite understand.
For example, the following code, despite being copied and pasted from a tutorial, will not generate any output in the terminal:
require 'rubygems'
require 'nokogiri'
require 'open-uri'
url = "http://stats.nba.com/player/#!/203096/stats/"
data = Nokogiri::HTML(open(url))
puts data
This leads to two questions:
Do I need to deploy this code via something like Heroku, before it can access URLs on the internet? Or will it only work locally until I do that?
Where, exactly, is the output supposed to be when I'm not openly running a Ruby console? I'm simply running the file in bash.
Yes, you can do it:
Goto C9.io and login
Click "Workspace" and choose "Custom Template"
mkdir scrape && cd $_
touch scrape.rb
Paste your code into scrape.rb
Run: ruby scrape.rb
There you go, everything should be fine.
Because you are using OpenUri to get the webpage, this will work on Heroku or on any machine. But you don't need to use Heroku, as you are already able to scrape nba.com on Cloud9.
You said the code you are using doesn't print to console, but it does for me.
It depends what you want to do with the data you parse using Nokogiri. Do you want to save it to a database or file? Look into writing files instead of using the puts command to print to the screen.
This code outputs to the console.
First make sure you installed the Nokogiri gem, and you have full Internet connectivity.
Second start debugging. To debug a program you can use a gem like byebug that gives you an interactive debugger at runtime. Otherwise, as your code is short, you can add a bunch of puts "point x" to actually see where your program halts or break.
Third pay attention to any error or warning you see on the screen.

Psych Error: `parse': (<unknown>): could not find expected

`':' while scanning a simple key at line 16 column 1(Psych::SyntaxError)
I'm working on a Rails application tutorial and have had the above error come up several times. When I was getting this error it seemed to only be a problem if I used one particular terminal window. I have since restarted the computer and now it is a problem regardless of what I do.
I went to the project on github and copied the information in 'secrets.yml' into my file in case that was the issue but doesn't help. In fact I'm not sure if that is even the file causing the problem because when I shift the code in that file and save it I still get the same error; at those times it points to a line that is now blank in the 'secrets.yml'
Update.
now I am getting this when i run this command
rails generate simple_form:install --foundation
-bash: $: command not found.
only change I did was start the code in 'secrets.yml' lower down, and i added a secret key to my .bash_profile, as it was suggested in tutorial and I had not done it before, and thought that might be the problem.
Update 2
shut down terminal and tied again and getting the original problem "psyche Error" Yes, thank you for your feedback, I too, although not sure why, suspect it is to do with the 'secret' file or could it be changes the tutorial told me to make to my .bash_profile. The instructions were not very clear, wondering if that is where I messed up. If that is so then I would not know how to fix that.
The Psych gem is the wrapper around libyaml and used within Ruby's YAML class, so it's not surprising you think it could be the 'secrets.yml'; a YAML config file. I'd stay the course and find what isn't being formatted correctly.

rails - hide 'source of your encoding' from terminal

I'm new to ROR and I"m having trouble with all the text that rails spits to the terminal window. Primarily, the html from my web pages get repeated in the terminal window and I'm really just wanting to see the important stuff like sql queries and error messages. I'm wasting a lot of time scrolling throughout the terminal window trying to find what I need b/c of all the HTML that fills up the screen.
Is there an option to disable the 'source of your encoding' output?
Thanks.
You could raise your log level
The rails guide gives a good example
http://guides.rubyonrails.org/debugging_rails_applications.html#log-levels
Make sure if you are in development to change it in config/environments/development.rb
I don't know if this will really solve your problem though because I am unsure of exactly what output you are looking for. A higher log level may throw out the baby with the bathwater.

Ruby on Rails - Stripping strange characters out of body in rake task

On my company's Rails website, we have a Twitter area where tweets from our social media team are displayed by a rake task. Basically the rake task uses the Twitter gem to import any new tweets into the database on a regular basis, and displays them from there. URL links in the tweet are converted to HTML links using the auto_link helper.
Always works fine, until now. All of the sudden, the links are broken and even wrongly highlighting the word right before the URL link. So in an example tweet that should look like this: "Please be safe St. Louis. Heat warning extended through August http://bit.ly/...", the word August is linked and the URL itself that follows is broken, as if there was something in between the last word and link breaking it...
Investigated the helpers, looked in the database for the tweet's text field to see if there was anything strange, even used the rails console to manually pull up the tweets, but everything looked okay. It wasn't until I went all the way into the tweet body's hex code that I saw...
Please be safe S
t. Louis. Heat w
arning extended
through August.
 http://bit.ly/
r5fXlz #heatpoca
lypse
So the culprit was that   being thrown into the space, when I deleted the culprit space and readded it manually in the database, the issue cleared up.
The only problem is, I don't understand why the tweet body is being imported like that, especially when it looks fine via the Rails console. As this is an older database, I noticed it was still using latin1 encoding in some areas with utf8 in others, and I was certain that converting all of that to UTF-8 would fix it, but it did not.
I went as far as tried to use a sanitation helper on the body before being imported, but that didn't work either.
Also tried a ruby gsub to strip the   out, but it didn't work.
Does anyone have any insight on how to solve this odd problem?
I was finally able to solve this by running the following specifically on the body string in the rake task...
Iconv.conv('ASCII//TRANSLIT', 'UTF8', tweet.body)
Odd, but it works. More information on using the above can be found here: ruby (1.8.7): How to get rid of non-printable chars while scraping?

How can I clear the rails console history

When I run rails c and press the up key when irb starts up, I can see the last commands I entered when my app dropped to irb after encountering a debugger command for the ruby-debug gem. I would not only like to clear these commands out, but I would like it if rails c would pull the last commands I issued during my last rails console session. I think it used to do this but I'm not sure what has changed. I'm on ruby 1.8.7 and rails 3.0.3 on Mac OS 10.6.5 if that helps.
Update
Ray's answer helped me out in the interim. Recently I did a bit more digging to find out more and realized that there are a number of conflicting issues.
IRB checks if you have a ~/.irbrc and if not falls back to /etc/irbrc as Ray mentioned. However, if you are using rvm there is another file to consider ~/.rvm/scripts/irbrc which just loads up ~/.rvm/scripts/irbrc.rb (note the .rb) if you have rvm_path set in your ENV (you should if using rvm).
Interestingly while ~/.rvm/scripts/irbrc.rb was based off of /etc/irbrc they are not the same and differ in a few ways. The most obvious way and easiest way to detect which one is being used on your system is their history file's name. If /etc/irbrc is being used your history file will be ~/.irb_history where as rvm's is ~/.irb-history (Note: _ vs -).
Hopefully this additional information will help you determine what you need to setup your system as you would like.
Pry Concerns
I've since stopped using debugger and have moved to pry-byebug which includes the pry gem. Pry is an alternative to IRB but can also be used along side and within it. The reason I was able to provide the above update is because I was trying to figure out how to keep their respective histories separate. For more information please see my answer to the SO question on "why does pry history keep cloberring irb history?". I've included links there to the known Github issue for Pry as well as my attempt to fix it.
I interpret you question as asking how to turn history on in the Rails Console and off in the Ruby debugger. If this isn't true, please clarify.
IRB, and by extension, the Rails Console, read from ~/.irbrc, or if that doesn't exist, /etc/irbrc, to startup and configure irb. Your history is typically written to ~/.irb_history, but that is dictated by the contents of your irbrc file. The /etc/irbrc on my Mac OS X is set up to write the history from irb, so perhaps you've created a local .irbrc that doesn't have history, or perhaps you have a syntax error in that file.
The debugger reads a file called .rdebugrc on startup. You can turn off history in debug by adding this line to ~/.rdebugrc:
set history save off
Turn it back on with:
set history save on
You could also set your debug output to go to a different file than irb reads from with the command:
set history filename
These also work from the debug prompt, but aren't persistent.
There are a number of tools to help improve the irb experience. Bond and hirb are promising.
Here is Comprehensive list of Irb Tools and some tips on directly editing the .irbrc file.
Hope this help!
Although a very old question I got here by google.
Turns out RVM slightly changed over time.
Currently my IRB history (using rvm) is stored here:
user#host:~$ ls ~/.rvm/rubies/ruby-2.4.2/.irbrc*
/home/user/.rvm/rubies/ruby-2.4.2/.irbrc
/home/user/.rvm/rubies/ruby-2.4.2/.irbrc_history

Resources