Wednesday, 20 April 2016

AJAX in Ruby On Rails: Sample Application


1) Create a new project. Generate Model and Controller

[shabbir@neutron Aptana Studio 3 Workspace]$ rails new ajax1

[shabbir@neutron Aptana Studio 3 Workspace]$ cd ajax1/

[shabbir@neutron ajax1]$ rails generate model Task name:string 

[shabbir@neutron ajax1]$ rake db:migrate 

[shabbir@neutron ajax1]$ rails generate controller tasks index


2) routes.rb

  resources :tasks

  root 'tasks#index'



3) task_controller.rb

class TasksController < ApplicationController
  def index
    @tasks = Task.all
  end

  def new
    @task = Task.new
  end

  def create
    @task= Task.new(task_params)
    @task.save
   
    @tasks = Task.all
  end     

  def edit
    @task = Task.find(params[:id])
  end
 
  def update
    @task = Task.find(params[:id])
    @task.update(task_params)

    @tasks = Task.all
  end

  def destroy
    @task = Task.find(params[:id])
    @task.destroy

    @tasks = Task.all
  end
 
  private
  def task_params
    params.require(:task).permit(:name)
  end
end



4)  Views

When the user clicks on the "New Task" link, an AJAX request is sent to the server since "remote: true' is set.
index.html.erb

<div>
    <%= link_to "New Task" , new_task_path, id: "new_link", remote: true %>
</div>

<div id="task_form" style="display:none">
</div>

<h2> List All Tasks </h2>

<div id="tasks">
<%= render 'list' %>
</div>



When the user clicks on the "Edit" or "Delete" links, an AJAX request is sent to the server since "remote: true' is set.
_list.html.erb

<table>
<tr>
    <th>Name</th><th colspan="3"></th>       
</tr>   

<% @tasks.each do |task| %>
    <tr>
        <td><%= task.name %> </td>
        <td><%= link_to 'Edit', edit_task_path(task), remote: true %></td>
        <td><%= link_to 'Delete', task, method: :delete, data: {confirm: 'Are you sure ?'}, remote: true %></td>
       
    </tr>
<% end %>
</table>






When the user submits the form, an AJAX request is sent to the server since "remote: true' is set.
  _form.html.erb

<%= form_for @task, remote: true do |f| %>

<%= f.label :name %>
<%= f.text_field :name %>

<%= f.submit "Save" %>
<% end %>



5) Javascript


When the user clicks on the "New Task" link, the form is displayed and the "New Task" link is hidden.  
new.js.erb

$("#new_link").hide();

$("#task_form").html("<%= j (render 'form') %>");

$("#task_form").slideDown();




After the Task is created, the "New Task" link is displayed ,the form is hidden and the list of tasks is updated.

create.js.erb

$("#new_link").show();

$("#task_form").slideUp();

$("#tasks").html("<%= j (render('list')) %>" );





When the user clicks on the "Edit" link, the form is displayed and the "New Task" link is hidden.  

edit.js.erb

$("#new_link").hide();

$("#task_form").html("<%= j (render 'form') %>");

$("#task_form").slideDown();



After the Task is updated, the "New Task" link is displayed ,the form is hidden and the list of tasks is updated.

update.js.erb
$("#new_link").show();

$("#task_form").slideUp();

$("#tasks").html("<%= j (render('list')) %>" );



When the user clicks on the "Delete" link, the task is destroyed and the list of tasks is updated.

destroy.js.erb
 $("#tasks").html("<%= j (render('list')) %>" );


4 comments:

  1. Problems with dovecot are not easy to fix. See here for mark email threads

    ReplyDelete
  2. Welcome to our SAHABATQQ
    Hanya di SAHABATQQ anda bisa lebih mudah menang

    POKER*DOMINO99 JUDI ONLINE TERBAIK MEJASAHABAT,NET

    Minimal deposit 20.000,- sudah bisa bermain disemua jenis game
    Untuk pendaftaran silahkan klik kolom daftar atau link di bawah ini boss
    http://mejasahabat,net/Register,aspx?lang=id

    * Promo Bonus ROLLINGAN 0.3% ( 5 hari sekali )
    * Nikmati Bonus Refferal 15 % ( Setiap putaran kemenangan refferal )
    * Bonus Ekstra Refferal 5 % ( 1 bulan sekali )

    Contact Us :
    * Pin BBM Android CS 1: SHBT99
    * Pin BBM Blackberry CS 2 : 2AE48042
    * Pin BBM Blackberry CS 3 : 2BD6A2E3
    * Whatsapp : +85581734021
    * Line : sahabatqq
    * Wechat : sahabatqq

    ReplyDelete
  3. Hey Guys !

    USA Fresh & Verified SSN Leads along with Driving License/ ID Number, AVAILABLE with 99.9% connectivity
    All Leads have genuine & valid information.

    **DETAILS IN LEADS**
    First Name | Last Name | SSN | Dob | Driving License Number | Address | City | State | Zip | Phone Number | Account Number | Payday | Bank Name | Employee Details | IP Address

    *Price for SSN lead $2
    *You can ask for sample before any deal
    *If anyone buy in bulk, we can negotiate
    *Sampling is just for serious buyers

    ==>ACTIVE, FRESH CC & CVV FULLZ AVAILABLE<==
    ->$5 PER EACH

    ->Hope for the long term Business
    ->Interested buyers will be welcome

    **Contact 24/7**
    Whatsapp > +923172721122
    Email > leads.sellers1212@gmail.com
    Telegram > @leadsupplier
    ICQ > 752822040

    ReplyDelete
  4. Hi Guy's

    Fresh & valid spammed USA SSN+Dob Leads with DL available in bulk.

    >>1$ each SSN+DOB
    >>3$ each with SSN+DOB+DL
    >>5$ each for premium fullz (700+ credit score with replacement guarantee)

    Prices are negotiable in bulk order
    Serious buyer contact me no time wasters please
    Bulk order will be preferable

    CONTACT
    Telegram > @leadsupplier
    ICQ > 752822040
    Email > leads.sellers1212@gmail.com

    OTHER STUFF YOU CAN GET

    SSN+DOB Fullz
    CC's with CVV's (vbv & non-vbv)
    USA Photo ID'S (Front & back)

    All type of tutorials available
    (Carding, spamming, hacking, scam page, Cash outs, dumps cash outs)

    SMTP Linux Root
    DUMPS with pins track 1 and 2
    WU & Bank transfers
    Socks, rdp's, vpn
    Php mailer
    Sql injector
    Bitcoin cracker
    Server I.P's
    HQ Emails with passwords
    All types of tools & tutorials.. & much more

    Looking for long term business
    For trust full vendor, feel free to contact

    CONTACT
    Telegram > @leadsupplier
    ICQ > 752822040
    Email > leads.sellers1212@gmail.com

    ReplyDelete