Ruby

Recently upgraded to Rails 5. There seems to be some compatibility issue, in particular when I run “rails c” or “rails s”.

The warning message is

Array values in the parameter are deprecated. Please use a String or nil. Fixed by applying the same join you see here to the line 11 of bin/spring:
Gem.paths = { 'GEM_PATH' => [Bundler.bundle_path.to_s, *Gem.path].uniq.join(File::PATH_SEPARATOR) }

How to push the code to the remote repository and deploy to Heroku

$git add -A

$git commit -m "made some improvements"

$git checkout master

$git merge test_branch_name

$git push

$git push heroku main

$heroku pg:reset DATABASE

$heroku run rake db:migrate

$heroku run rake db:seed

There you go…


Question:

I would like to take information from another website. Therefore (maybe) I should make a request to that website (in my case a HTTP GET request) and receive the response.

How can I do this in Ruby on Rails?

If it is possible, is it a correct approach to use in my controllers?

Answer #1:

You can use Ruby’s Net::HTTP class:

require 'net/http'

url = URI.parse('http://www.example.com/index.html')
req = Net::HTTP::Get.new(url.to_s)
res = Net::HTTP.start(url.host, url.port) {|http|
  http.request(req)
}
puts res.body

Answer #2:
You could write a method to do this in the controller

  def api_request(type , url, body=nil, header =nil )
    require "net/http"
    uri = URI.parse(url)
    case type
    when :post
      request = Net::HTTP::Post.new(uri)
      request.body = body
    when :get
      request = Net::HTTP::Get.new(uri)
    when :put
      request = Net::HTTP::Put.new(uri)
      request.body = body
    when :delete
      request = Net::HTTP::Delete.new(uri)
    end
    request.initialize_http_header(header)
    #request.content_type = 'application/json'
    response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') {|http| http.request request}
  end

Answer #3:
A general example of using Ruby Net::http
 url = URI.parse('https://google.com')
 req = Net::HTTP::Get.new(url.request_uri)
 http = Net::HTTP.new(url.host, url.port)
 http.use_ssl = (url.scheme == "https")
 response = http.request(req)
 puts response.body

 


Question: Should people store JSON object in the database file as a string?

Size is not so much of an issue, the ability to query and maintain the data however is.

If, for example, Greenhaven Press decides they want to change their name to Greenhaven Press International, you’ll have to find the record, deserialize it, change it, serialize it, pump it back into the database.

Consider this: does storing these objects as serialized data offer you a clear added value over storing it in a relational form? If the answer is no, then it might not be worth the hassle.

UPDATE

As far as your update of your question goes: I’m inclined to say no, it makes little or no difference. Whether you update one field or all of them in this json string is irrelevant because the whole process is identical.

Don’t forget that your requirements might change; even though you’re using json on the client side now doesn’t mean you’ll need json in the future. Storing your data in a relational form guarantees technology-independence while preserving relationships, data constraints and queryable metadata: this is where the true value of a relational db lies. Discarding those advantages will neither give you a performance gain nor make your application more scalable or flexible.

 ————————————————————–
Question: how to access a Rails app from another computer on the same LAN?
rails server -b myipaddress -p 3000
Your firewall may block 80; so try another port instead.
——————————————————————-
Question: issue with loading a gem?

/usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require’: cannot load such file — cassandra (LoadError)

Answer:Rubygems are separated by ruby version, so you need to reinstall the Cassandra gem:

gem install cassandra

Make sure that gem is pointed to the 1.9 gem.

The main reason you need to do this is to rebuild the C extensions against the 1.9 libraries.

———————————————————————-
When using Sublime Text for any scripting language such as Ruby, sometimes you will need to be flexible and change the build system (e.g., path to the interpreter).
Rails authentication gems: Devise and devise_token_auth.
——————————————————————-
Question: Where is Rails Helper available?

Answer:

Helpers

The bottom line answer is the application_helper is available in all your views.

Rails actually uses helpers all over the place – in everything from the likes of form_for to other built-in Rails methods.

As Rails is basically just a series of classes & modules, the helpers are loaded when your views are rendered, allowing you to call them whenever you need. Controllers are processed much earlier in the stack, and thus you have to explicitly include the helpers you need in them

Important – you don’t need to include the ApplicationHelper in your ApplicationController. It should just work

———————————————————————-
Rails 5. 2 has capability to include node.js package (via Yarn and Webpacker).
Question: How to update rbenv (rbenv is out of date) to include all ruby build versions
cd ~/.rbenv
git pull
 and
cd ~/.rbenv/plugins/ruby-build/
git pull
———————————————————————————————————————–
require_relative in Ruby.

require_relative complements the builtin method require by allowing you to load a file that is relative to the file containing the require_relative statement.

For example, if you have unit test classes in the “test” directory, and data for them under the test “test/data” directory, then you might use a line like this in a test case:

require_relative "data/customer_data_1"
Some issue with bcrypt (3.1.13) installation
bcrypt 3.1.13 contains some assembly code and compilation of the native extension may incur errors on some platforms, e.g., Raspberry Pi. The makefile needs some tweaks. Version 3.1.12 should be fine.
Rails on Heroku (Ruby 2.6.5; Rails 6.0.3.2) and use of PostgreSQL for development
It seems that Heroku no longer support SQLite. If your development setup uses SQLite, you might have difficulty deploying your app to Heroku. To maintain consistency across all environments, you’d better start with PostgreSQL database even for the development environment.
$rails new -d postgresql
Open your Gemfile, and remove gem ‘sqlite3’ and add gem ‘pg’, then run bundle install.
Go to your config/database.yml and check your database setting (e.g., development). If you are on Windows (for example, 10), you may want to first install postgreSQL (e.g., version 12). First of all, start pgAdmin 4, and a browser window will then pop up. Follow the steps: 1. Create a new server, 2, create a new database on the server created and the database name could be consistent with whatever in database.yml (i.e., database name, user name, and password).
There is a sidenote on Heroku (https://devcenter.heroku.com/articles/sqlite3).
If you’re not using Rails, you may need to manually add the postgres addon by running
$heroku addons:create heroku-postgresql