Rails
Deploy your Rails API onto Heroku
Go to your Heroku Dashboard. Click “new”-> “create new app”
Give your app a name and click “Create app”
There are several ways to deploy
- Heroku CLI – push your project up to Heroku from your command line
- Github – Heroku is connected to github, so everytime github is updated your app is
- Dropbox connected
- Container Registry
We will be using Heroku CLI. If you need to install use these instructions
By default Heroku ONLY supports the master branch. If you want to push up a branch other than master follow these instructions.
Add Heroku as a remote to your project
heroku git:remote -a name-of-your-heroku-app
Push your project to Heroku
git push heroku master
Did you just get an error? Did you use sqlite3 for your project?
remote: An error occurred while installing sqlite3 (1.3.13), and Bundler cannot
remote: continue.
remote: Make sure that `gem install sqlite3 -v ‘1.3.13’` succeeds before bundling
remote:
remote: In Gemfile:
remote: sqlite3
remote: !
remote: ! Failed to install gems via Bundler.
Migrate and seed your database
In your command line run the following commands.
heroku run rake db:migrate
heroku run rake db:seed
Test your API using Postman
To get the address, just go back to the Heroku dashboard and click on “Open app” to get the link to your online api
If you made it this far, your API should be deployed and now test it as you did locally (you did test locally didn’t you???).
The reason you need postman is if you setup your Rails API correctly when you query your site via browser you should get an error
This appname-api.herokuapp.com page can’t be found
but if you connect to it via postman you should get back the appropriate response
Converting Rails from sqlite3 to PostgreSQL
Your mod 3 and 4 projects were probably done using sqlite3. If you did your mod5 project in sqlite3, tsk tsk!
- edit your Gemfile
- comment out sqlite and add gem pg
- run “bundle install”
- edit
config/database.yml
- Change your databases names, as below.
- run
rake db:create
- run
rake db:migrate
- run
rake db:seed
(if you have a seed file) - Now test your app, and you are now using Postgres!
How do I know that it worked?
- Go to the little Postgres elephant, and click him
- Press stop
- If your site does not work, then you migrated successfully. Refresh your browser if needed
- Now start up Postgres so you can continue developing!
uninitialized constant AuthorizeApiRequest::JsonWebToken
Was a weird error.I build fully functioning apps locally before deployment. Locally when running rails server, this error never came up.
When I deployed the app to heroku, I then started receiving that error.
I thought adding “require ‘jwt'” might help, but it didn’t.
This comment by MikaOY pointed me in the right direction:
“Turns out it isn’t a problem with the AuthO implementation, just that libs/ is not included by auto-loaded by default in some versions of Rails.”
Well, I looked in application.rb and I was already autoloading lib. But it pushed me in the right direction.
I specifically had to require_relative the rb file that contained JsonWebToken. Still looking into why that is so, especially since I have a working instance locally. It could just be something with heroku. But hope this helped someone.
Bypass authentication for signup page in Rails API
I added authentication on my rails api.
Works great, only authenticated users can now call on the api.
Now my problem was that I am unable to signup, since it requires authentication. Catch-22.
how to bypass?
Authentication is being inherited from application_controller.rb
for the signup controller, I just inherited directly from ActionController::API
This was helpful if you need to setup the api and you need assistance.
Rails: stop ActiveRecord::RecordNotFound from breaking app
Was building a rails api backend, but when I would search for a record that was not found, the app would show the error. That is great for testing, but I wanted to handle the error without a 404 since this is an api and wanted to send a specific json message.
Found the solution as such:
class YourController < ActionController::Base rescue_from ActiveRecord::RecordNotFound, with: :dude_wheres_my_record def show # your original code without the begin and rescue end def dude_where_my_record # special handling here end end
Found on stack overflow thanks to noodl
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))