Posts

Showing posts from September, 2019

Add CORS to Nginx on AWS Elastic Beanstalk

Image
Apache Add the following block to your  .htaccess  file: <FilesMatch ".(eot|otf|ttf|woff|woff2)"> Header always set Access-Control-Allow-Origin "*" </FilesMatch> Nginx Add the following location block to your virtual host file (usually within the server directive) and reload Nginx: location ~* \.(eot|otf|ttf|woff|woff2)$ { add_header Access-Control-Allow-Origin *; } Invalidate CloudFront Distribution Once the changes have been made you will need to  invalidate the CloudFront cache  with a path of “/*”. Integrating to CloudFront service It’s simple to config and use CloudFront (a CDN service) for Rails app.  Just one thing from my experience, regarding to CORS issue on EB when your CSS want get loading font files or font-awesome. Because EB run your app with Nginx server serve public static files, then work around would be overwriting web server config in Nginx with  add_header Access-Control-Allow-Origin ‘*’  by crea

Add auto increment column in Postgres in Rails

class AddAutoIncrements < ActiveRecord::Migration def self.up add_column :designs, :custom_id, :integer execute <<-SQL CREATE SEQUENCE custom_id_seq START 1; ALTER SEQUENCE custom_id_seq OWNED BY designs.custom_id; ALTER TABLE designs ALTER COLUMN custom_id SET DEFAULT nextval('custom_id_seq'); SQL end def self.down remove_column :designs, :custom_id execute <<-SQL DROP SEQUENCE custom_id_seq; SQL end end OR Added by Active record: class AddAutoIncrements < ActiveRecord::Migration def self.up add_column :users, :custom_id, :integer ActiveRecord::Base.connection.execute("CREATE SEQUENCE users_id_seq") ActiveRecord::Base.connection.execute("ALTER SEQUENCE users_id_seq OWNED BY users.id") ActiveRecord::Base.connection.execute("ALTER TABLE users ALTER COLUMN id SET DEFAULT nextval('users_id_seq')") end end def self.down remove_column

ERROR: null value in column "id" violates not-null constraint

This is easier: ActiveRecord :: Base . connection . tables . each do | t | ActiveRecord :: Base . connection . reset_pk_sequence !( t ) end If sequence missing then create new sequence to all database table:           ActiveRecord::Base.connection.tables.each do |t|      ActiveRecord::Base.connection.execute("CREATE SEQUENCE #{t}_id_seq")   end

Setup FREE SSL using Let's Encrypt.

Setup SSL There are multiple services out there offering SSL certificates for your domain, many are paid and a couple are free. Setup SSL no matter what service you use.  Let's Encrypt  is very easy, and free, to use. Let's Encrypt is a non-profit organization backed by many tech giants. Installation Run the following commands, and follow the prompts. sudo apt-get update sudo apt-get install software-properties-common sudo add-apt-repository ppa:certbot/certbot sudo apt-get update sudo apt-get install python-certbot-nginx sudo certbot --nginx sudo certbot renew --dry-run If you go to your Vultr instance now using  HTTPS , you will see a secure website. Renewal Your newly issued certificate will expire, and will need to be renewed periodically. The easiest way to do this is to set up a cron job. sudo crontab -e 30 2 * * 1 /usr/bin/certbot renew As a final, but optional step, reboot your VM and make sure all services restart as expected.

Rspec for scopes in rails

 describe "Scopes" do       it "should be active with status_id 3" do       User.active.should include(@user)       end       it "should not be suspended without status_id 4" do           User.suspended.should_not include(@user)       end       it "should not be inactive without status_id 5" do           User.inactive.should_not include(@user)       end       it "should not be hidden without status_id 6" do           User.hidden.should_not include(@user)       end       it "should not be deleted without status_id 7" do           User.deleted.should_not include(@user)       end   end