Uncategorized
Render files and images for Rails 5 api only
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
Solution:
#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
end
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
getting this error when using fetch to connect to my rails api
fetch(`/products/${dataid}/description`)
.then(res=>res.json())
.then(json=>console.log(json))
in the browser I was able to get to the page
http://127.0.0.1:3000/products/11/description
answer
rails action was rendering basic text not json, so I had to change fetch
***Rails
def description
product=Product.find(params[:id])
if product
if product.description
render plain: product.description
else
render plain: “No description”
end end end
***JS
fetch(`/products/${dataid}/description`)
.then(res=>res.text()) //<————————————-
.then(json=>console.log(json))
Could not find an executable [“phantomjs”] on your path.
ERROR:
Got this error when running rspec.
1.2) Failure/Error:
raise Dependency::NotFound.new(
“Could not find an executable #{@executables} on your path.“)
Cliver::Dependency::NotFound:
Could not find an executable [“phantomjs”] on your path.
FIX:
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 https://github.com/learn-co-curriculum/your-own-js-and-css-in-rails/issues/7
Node notes : on require with no js file in local path
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
io.emit vs socket.emit
io.emit lets the server send an event to all the connected clients at once. This is useful when you need to send some data to everyone. This is something we want everyone to be able to see.
socket.emit sends an event to a single socket. This is great when you need to send an event to a single user. Example: This is why we use socket.emit when sending out a greeting message. It only needs to go to that one user.
socket.io notes
Good advice on learning programming from scratch
I see similar advice over and over, learn one core language, C++ or Java; Learn one scripting language, python, etc; Learn Linux; Build Code; Get job: Im reposting this here because out it’s well layed out
I am 100% sure that if you are intelligent you can do this, and that it will likely be financially worth it.
I will teach you how to obtain the skills to get a job at an amazon or google type company (at LEAST 90k starting salary):
- You must be a competent programmer in a main stream lower level language (say c++ or java) as well as a scripting language (python, bash, etc.).
-Learn C++ from C for dummies follow by something like “accelerated c++” or Stanley Lippman, Essential C++, or Absolute C++ -Learn python similarly and by simply reading the python docs online. -You must also practise using these languages which you will do by doing your own simple projects as well as programming solutions to contest problems as I outline in further steps.
- You must understand the unix operating system. -Install linux (say ubuntu) and use it as your primary operating system. Start using the command line as often as possible. -Read a basic book about unix “teach yourself unix in 24 hours” and “unix in a nutshell” should get you going. Make sure you know about grep and chmod
- Data structures. Understand and be able to describe: -binary search trees (including self balancing such as red-black and AVL) -hash tables -stack, queue, array, priority queue.
-Read the relevant sections in Sedgwick’s “Algorithms in C” or any data structures in C book. (Internet reviews should help).
- You must be able to solve ‘difficult’ algorithmic problems using techniques such as: -dynamic programming -divide and conquer
Buy Algorithm Design Manual and Introduction to Algorithms by Cormen, et al (CLRS). Work through these books slowly. You will have to take time to master everything in them and ask questions as necessary. They are not simply understood.
- Understand basic networking (how the internet works). Key concept examples: DNS, tcp/ip
There’s probably an article on how stuff works or just get a book about this stuff if you have to.
- Understand how a program is built. What an assembler, compiler and linker are. Just google this stuff. To truly understand this fully you should disassemble a program. Ollydbg is a program that can do this. A good place to try out your skills is osix.net . They have a series of ‘reverser’ challenges that will teach you about this (passing even the first level or 2 is good enough).
- Object oriented Design and design patterns:
- You should have some sense of OOP (object oriented programming) from learning C++ through the books above. Learn more if necessary by finding an OOP specific book. -Read ‘head first design patterns’ (CRUCIAL BOOK)
- Other tidbits: SQL helps (a language used to query a database) sqlzoo.net should get you started. Functional programming (a language like Scheme or Haskell) is usually helpful in learning how to program well. Structure and interpretation of programs is a good book.
Projects I’d implement: -A simple mysql backed website. Just make it so you can click a button and that will initiate a python script which will insert something into your db as an example. Simple, but shows you how everything works together. -Work through the ‘geek’ challenges on osix.net as well as the ‘reverser’ challenges. -Try to work through about 1/5 or 1/10 of the challenges in ‘algorithm design manual’ -Enter a few programming contests online the ‘google ai challenge’ was really good. -Try to make a simple version of anything you are interested or anything you have ever wondered how it could/would work.
Remember practise, practise, practise. Everyday read and implement.
Free up space Mac OS X
Someone asked if I could assist with getting some space back. They had 256mb free. WOW.
Looked at finder and they had 11gb of pics, but 36gb of other stuff
used the link below to free up space.
removed some stuff from ~/Library/Caches
deleted downloads
ended up having enough space to make the laptop usable.
http://osxdaily.com/2012/11/11/tips-free-up-disk-space-mac-os-x/
Samsung watch brings down AIX servers on network
Op connects his samsun watch to his wireless network, AIX servers stop working.
Problem 1: seems watch names itself “localhost”
Problem 2: DHCP overwrote localhost DNS entry of 127.0.0.1 to samsung watch’s IP
Problem 3: Oracle changed the order of how they look up the local host by going to DNS first rather than it’s own host file entry.
https://www.reddit.com/r/sysadmin/comments/3x8skg/tifu_by_connecting_my_samsung_watch_to_our/
Find what SQL server features and components are installed
There is a feature in SQL installer that can tell you what you have installed
Check your install media or SQL Server Installation Center if it is installed locally, then run the link for “Installed SQL Server features discovery report”.


Thanks to:
- ← Previous
- 1
- …
- 5
- 6