Skip to main content

How to create Drag and Drop functionality in Ruby On Rails?

I have seen many developers that are looking for creating "Drag and Drop" functionality in Ruby On Rails technology. So I'm going to brief about a sample to create and implement this functionality using JQuery Rails, JQuery UI Rails and Rails Sortable gems. Given below are simple steps required to create the rails application:

Ruby On Rails Programming

How to create Drag and Drop functionality in Ruby on Rails?

First of all we will create a simple application first to create the products.
  1. The first Step we required to create the Rails Application.
    $ rails new SampleSortRails
  2. By using the scaffold we will generate the the MVC components needed for Simple Products application form
    $ cd SampleSortRails
    $ rails g scaffold Product name:string description:text quantity:integer price:integer sort:integer
  3. Create the Products database table:
     $ rake db:create
    $ rake db:migrate
    Rails uses rake commands to run migrations. Migrations are Ruby classes that are designed to make it simple to create and modify database tables. Rails uses rake commands to run migrations.
  4. Then we need to set routes of our application.
    $ rake routes
         Prefix Verb   URI Pattern                  Controller#Action
            products GET    /products(.:format)          products#index
                     POST   /products(.:format)          products#create
         new_product GET    /products/new(.:format)      products#new
        edit_product GET    /products/:id/edit(.:format) products#edit
             product GET    /products/:id(.:format)      products#show
                     PATCH  /products/:id(.:format)      products#update
                     PUT    /products/:id(.:format)      products#update
                     DELETE /products/:id(.:format)      products#destroy
                root GET    /                            products#index
    sortable_reorder POST   /sortable/reorder(.:format)  sortable#reorder
  5. In the routes.rb file then we will set the routes of our application.
    Rails.application.routes.draw do
      resources :products
      root 'products#index'
    end
    
  6. We have made the simple application for products CRUD operation for adding the Drag and drop functionality in the Application we need to follow some steps and need to do Setup .
    First of all we need to add the gem files in the application in the gem file.
    gem 'jquery-rails'
    gem 'jquery-ui-rails'
    gem 'rails_sortable'
  7. Then we need to run the bundle install command.
     $bundle install
  8. And then We need to add the following to the asset pipeline in the application.js:
    //= require jquery
    //= require jquery_ujs
    //= require jquery-ui/widgets/sortable
    //= require rails_sortable
  9. We have already run the migration command our database migration command look like this.
    class CreateProducts < ActiveRecord::Migration[5.1]
      def change
        create_table :products do |t|
          t.string :name
          t.text :description
          t.integer :quantity
          t.integer :price
          t.integer :sort
    
          t.timestamps
        end
      end
    end
    
  10. The product model look like this
    class Product < ApplicationRecord
      include RailsSortable::Model
      set_sortable :sort
    end
    
  11. In the controller index method we only need to change one line.
    class ProductsController < ApplicationController
      def index
        @products = Product.order(:sort).all
      end
    end
  12. Normally The view of index page always look like this we only need to change some part of code
    class="sortable">
        <% @products.each_with_sortable_id do |product, sortable_id| %>
          id="<%= sortable_id %>">
            <%= product.name %>
            <%= product.description %>
            <%= product.quantity %>
            <%= product.price %>
            <%= product.sort %>
            <%= link_to 'Show', product %>
            <%= link_to 'Edit', edit_product_path(product) %>
            <%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %>
            
          <% end %>
        
      
  13. In the Application.js file we need to add the Javascript function
    $(function() {
      $('.sortable').railsSortable();
    });
  14. Then we need to start the server.
    $rails s
Feel free to ask any queries.
Source: http://www.cryptextechnologies.com/blogs/how-to-create-drag-and-drop-functionality-in-ruby-on-rails

Comments

Popular posts from this blog

GraphQL With Ruby

Now a day’s most of the web or mobile applications fetch data from server which is stored in a database. REST API provides an interface to stored data that require by the applications. GraphQL is a query language for REST API's not for server databases. It is database agnostic and effectively can be used in any context where an API is used. GraphQL provide platform for declarative data fetching where client need to specify what data needs from API in response. Instead of multiple endpoints that return fixed data structures, a GraphQL server only exposes a single endpoint and responds with precisely the data a client asked for. GraphQL minimizes the amount of data that needs to be transferred over the network and improves applications operating under these conditions. Introduction to GraphQL API on Ruby on Rails Start with adding gem in Gemfile gem ‘graphql’ Run command bundle install Run command rails generate graphql:install Above command will add gr

Best In Place Gem In Ruby On Rails Tutorial

The best_in_place gem is the easiest solution for in place editing in Ruby on Rails. This gem provides functionality of “in place editing” in ruby on rails without writing any extra ajax code. It supports text inputs, textarea, select dropdown, checkboxes, jQuery UI Datepickers, etc. Also Displays server-side validation Installation Steps of “best_in_place” Gem : Installing best_in_place is very easy and straight-forward. Just begin including the gem in your Gemfile: gem ‘best_in_place’ After that, specify the use of the jquery and best in place javascripts in your application.js, and optionally specify jquery-ui if you want to use jQuery UI datepickers: //= require jquery //= require best_in_place //= require jquery-ui //= require best_in_place.jquery-ui Then, just add a binding to prepare all best in place fields when the document is ready: $(document).ready(function() { /* Activating Best In Place */ jQuery(".best_in_place").best_in_place(); });

Data scraping in Ruby on Rails using Nokogiri and Mechanize Gem

What is Data scraping? Website/Data  Scraping  is a technique to operating large amounts of  data  from websites whereby the  data  is extracted and displayed in own sites or it can be stored to a File/Database. Data scraping is basically used where the websites does not provides API. Some Applications do not provide API to collect records. For the same , Data Scraping technique is used. The data can be scraped using Nokogiri Gem. The steps are required: Add the gem “gem ‘nokogiri’, ‘~> 1.8’, ‘>= 1.8.1'” . Then run the bundle install Add the “require ‘nokogiri'” , “require ‘open-uri'” line where you will write the code for the scraping. The controller of the page will look like below: The view of the code of view page will look like : The result in our application will look like: Mechanize Gem in rails The Mechanize library is used for automating interaction with websites. Mechanize automatically stores and sends cookies, follo