Posts

RUN RAILS SERVER ON PORT 443

If we want to run the rails application in port 443 :- I have added the below line in /config/environments/production.rb: config.force_ssl =true Now I have tried the below attempts: Start rails on 3000 SSL=true rails s -e production -p 3000 It runs rails on https://project.com but 404 error on http://project.com Start rails on 443 and mentioned the same port in script:

CURL COMMAND ON RUBY ON RAILS

HOW TO USE CURL ON RUBY ON RAILS? LIKE THIS ONE curl -d 'params1[name]=name&params2[email]''http://mydomain.com/file.json' Just in case you don't know, it requires 'net/http' require 'net/http' uri = URI.parse("http://example.org")

AUTOCOMPLETE WITH AJAX IN RAILS

Image
Autocomplete with Ajax in rails index.html.erb page:-  <%= f.text_field :store_name, id: "store_shop" %>
controller :-class StaticController < ApplicationController
  def index; end

  def search
    if params['search'].size > 2
        result = search_shop(params['search'])
      found  = result.present? ?  result.map{|x| x["name"]} : result
      render :json => found
    else
      render :json => []
    end
  end
end
javascript :- // calling the third api from autocomplete
<script>
  $(function() {
    function log( message ) {
      $( "<div>" ).text( message ).prependTo( "#log" );
      $( "#log" ).scrollTop( 0 );
    }

    $( "#
store_shop" ).autocomplete({
      source: function( request, response ) {
       $.ajax({
          type: "get",
          url: "/static/brewery/",
          data: { search: request.term },
          dataType: "json",
          success: function…

psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

If we are getting error after creating database in postgres like this :-psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"? Then use following link :- https://stackoverflow.com/questions/42653690/psql-could-not-connect-to-server-no-such-file-or-directory-5432-errorsudo chmod 777 -R /var/run/postgresql/Then restart the postgres:-  sudo service postgresql restart orsudo systemctl start postgresql@9.6-main

Building native extensions. This could take a while... ERROR: Error installing mysql: ERROR: Failed to build gem native extension.

When we are getting problem with mysql like :-Building native extensions. This could take a while... ERROR: Error installing mysql: ERROR: Failed to build gem native extension.If we want to solve this problem so firstly we need to uninstall the mysql :-
Uninstall mysql :- https://www.linuxhelp.com/how-to-remove-mysql-package-completely-on-ubuntu-16-04/Then we need to install the mysql :-
https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-16-04
Then install the mysql gem :-
gem install mysql 
After that run the below command :-
sudo apt-get install libmysqlclient-dev

CREATE TABLE "schema_migrations" ("version" character varying(255) NOT NULL)

When we are getting problem with Postgres like :-: CREATE TABLE "schema_migrations" ("version" character varying(255) NOT NULL)Solution :- 
Now you can follow the link below :- https://github.com/influitive/apartment/issues/17

RAILS: UPLOADING PHOTOS VIA AMAZON S3 AND PAPERCLIP (UPLOADING FILES TO S3 IN RUBY WITH PAPERCLIP)

Set up Ruby on Rails with Paperclip and S3 using AWS SDK
Uploading Files to S3 in Ruby with Paperclip Paperclip requires the following gems added to your Gemfile. If your paperclip version is 5.1.0 then we are using 'aws-sdk' version 2.3.# Gemfile
gem 'paperclip'
gem 'aws-sdk', '~> 2.3'
or our paperclip version is 4.1.0 then we need to use 'aws-sdk' version < 2  (note: add version less than 2.0 otherwise you will get paperclip error)gem 'paperclip'
gem 'aws-sdk', '< 2.0'


Run bundle install and restart the Rails server after modifying the Gemfile. Then run the command :-rails generate paperclip user image Define the file attribute in the Modelclass User < ActiveRecord::Base
    has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
      validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end
Migration…

HOW TO CALL RAILS API FROM SIMPLE HTML PAGE.

Calling the rails API from simple HTML page. Step 1:  Create new form in the HTML page. index.html.erb<form action="javascript:void(0)">
  <input type="text" name="first_name" id="first_name">
  <select name="resident" id="resident">
    <option value="">Select</option>
    <option value="true">true</option>
    <option value="false">false</option>
  </select>
  <textarea type="textarea" name="notes" id="notes"></textarea>
  <input type="file" name="image" id="image">
  <button type="button" name="submit" id="form_submit_api">Send Data</button>
</form> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigi…

AWS::S3::ERRORS::ACCESSDENIED (ACCESS DENIED):

Image
Paperclip S3 AccessDenied Aws::S3::Errors::AccessDenied (Access Denied): Assuming you are using AWS’s IAM and you created a dedicated User for these uploads.If you get this error when trying to upload to S3, you need to assign this IAM User the “AmazonS3FullAccess” Policy.

GENERATE PDF FILE IN RAILS

Code Generating PDFs From HTML With Rails Using Wicked_pdf with Rails 5 Step 1 Install the required gemsgem 'wicked_pdf', '~> 1.1'  
gem 'wkhtmltopdf-binary'   
then generate the initializerrails g wicked_pdf               Step 2(optional) You need to register the pdf mime type. This is done in the initializer. This is required for older rails version only #config/initializers/mime_types.rbMime::Type.register "application/pdf", :pdf Step 3 Setup the controller to render pdf format. In this setup you have the options to configure so many item depends on you requirement. At the moment, only a few items need to be setup for me. #controllers/yours_controller.rbdef show
   .
   .
   .
   respond_to do |format|
    format.html
    format.pdf do
      render pdf: "Your_filename",
      template: "yours_controller/show.html.erb"
      layout: 'pdf'

    end
   end
end
Step 4 Create and setup the view part of the application First we create …

UNINITIALIZED CONSTANT PAPERCLIP::STORAGE::S3::AWS

Set up Ruby on Rails with Paperclip and S3 using AWS SDK Paperclip requires the following gems added to your Gemfile. If your paperclip version is 5.1.0 then we are using 'aws-sdk' version 2.3.gem 'paperclip'
gem 'aws-sdk', '~> 2.3'
or our paperclip version is 4.1.0 then we need to use 'aws-sdk' version < 2  (note: add version less than 2.0 otherwise you will get paperclip error)gem 'paperclip'
gem 'aws-sdk', '< 2.0'


Run bundle install and restart the Rails server after modifying the Gemfile.

WE CAN FOLLOW THE LINK BELOW :-

https://github.com/thoughtbot/paperclip/issues/2388
https://coderwall.com/p/vv1iwg/set-up-ruby-on-rails-with-paperclip-5-and-s3-using-aws-sdk-v2

DEPLOYING PROJECT USING CAPISTRANO (CAPISTRANO IN RAILS)

Image
We can Deploy project on Digital Ocean, AWS, Transip, Linode server ... using CapistranoWhat is Capistrano:- Capistrano is a remote server automation tool.

It supports the scripting and execution of arbitrary tasks, and includes a set of sane-default deployment workflows.

For that we have to follow simple steps :- How to Install Capistrano We can use Capistrano in RAILS 4, RAILS 5 as well as RAILS 2 and 3 . If we use RAILS 4 and 5 , then we can follow these steps :- 1.  Gemfile:- group :development do
             gem "capistrano"
             gem 'net-ssh'
             gem 'capistrano-bundler'
             gem 'capistrano-rails'
             gem 'capistrano-rvm'
             gem 'capistrano-sidekiq'
       end


2.   Run the bundle      cd /path/to/your/projectbundle 3.   Prepare your Project for Capistrano       capify .       This will create:       Capfile in the root directory of your Rails app       deploy.rb file in the config directo…