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 samejoin
you see here to the line 11 ofbin/spring
:
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.
/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.
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
cd ~/.rbenv
git pull
and
cd ~/.rbenv/plugins/ruby-build/
git pull
require_relative
complements the builtin methodrequire
by allowing you to load a file that is relative to the file containing therequire_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"

How to push the code to the remote repository and deploy to Heroku
There you go…