Latest Event Updates

Render files and images for Rails 5 api only

Posted on

I had built a rails 5 api only app. By installing the app only version of rails, I lost the view layer of rails. No app/assets, or public files.

This is of course the intended design for an api application but my projects also needed to reference locally stored images. How to overcome?

Example: I need to fetch http://mysite/images/image5.jpg


#config/routes.rb . #add a route

get ‘images/:id’, :to => ‘images#show’

#app/controllers/images_controller.rb #create this file, make class ImagesController,

#add method below

def show
id = params[:id]  #this will get the filename
send_file Rails.root.join(“public”, “#{id}.jpg”), type: “image/gif”, disposition: “inline” .          #send the file requested, files stored in public


This is a word around and there could (probably is) a better way, but it worked to get the project going. During refactor, we can change this and look for a better solution of need be. This was better than having to start a new rails5 app from scratch.


Uncaught (in promise) SyntaxError: Unexpected end of JSON input

Posted on

getting this error when using fetch to connect to my rails api



in the browser I was able to get to the page




rails action was rendering basic text not json, so I had to change fetch


def description
if product
if product.description
render plain: product.description
render plain: “No description”
end end end


.then(res=>res.text()) //<————————————-

Could not find an executable [“phantomjs”] on your path.

Posted on


Got this error when running rspec.

1.2) Failure/Error:


              Could not find an executable #{@executables} on your path.)


            Could not find an executable [“phantomjs”] on your path.



gem 'phantomjs', :require => 'phantomjs/poltergeist'

I added this to my gem file, under group :development, :test do and ran bundle install

After that this error didnt come up

answer found on

Ah grep you are a time saver

Posted on

Searching for a function valid_move that was made in one of my tttt dirs 

How to quickly find it with out manually going to each file?

grep -R “valid_move” ttt*

MONGODB: What is a projection?

Posted on Updated on

A projection is when you search for documents in MongoDB but only get back the fields that you request.

example records   {name: “Tom”, age: “35”, zipcode: “10003”,_id:778789754}

using projection for name and zipcode: {name: “Tom”,  zipcode: “10003”}


var mongo = require(“mongodb”).MongoClient;
var url = “mongodb://localhost/addressbook”;
var collectionName = “friends”;
if(err) console.log(err)//throw err;
var collection = db.collection(collectionName);
collection.find({},{name: 1, age: 0,_id:0,zipcode: 0}).toArray( function(err,documents){
if(err) console.log(err)//throw err;


node notes: require() caches module.exports

Posted on Updated on

if you are importing a file that instantiates a new object

when you import the file again as a 2nd object you would expect … a new object

but your 2nd variable will be the same object as the first variable, even though a “new” constructor was used. why?

Node caches module imports , it wont load the file again so no new instance!!!


// file greet3.js
function Greetr(){
  this.greeting=”Hello World from greet3″
module.exports = new Greetr();


var greet3 = require(“./greet3.js”);
greet3.greeting = “NEW GREETING!!!!”
var greet3a = require(“./greet3.js”);
Hello World from greet3
how to resolve??
in the file being exported, greet3.js, send the constructor not the new instance
module.exports = Greetr;
var Greet3a = require(“./greet3.js”);  //you capitalize greet3a to tell its constructor
var grtr = new Greet3a();

Node notes : on require with no js file in local path

Posted on

if you require, for example  “greet”, and greet.js is not found, node will look for folder called “greet” and then look for a file called index.js