CRUD Operation in Ruby On Rails using JS resuest.

class Referral::ReferralConsultsController < ReferralBaseController
  before_filter :get_consults, only: [:index]
  before_filter :find_consult, only: [:edit, :update]
  autocomplete :referral_consult, :doctor_name

  def index
    respond_to do |format|
      format.js{}
      format.html{ redirect_to preferences_ic_bc_reports_path(current_ic) }
    end
  end

  def new
    respond_to do |format|
      format.js{ @consult = ReferralConsult.new }
      format.html{ redirect_to referral_ic_patients_path(current_ic) }
    end
  end

  def create
    respond_to do |format|
      format.js{
        @consult = ReferralConsult.new(params[:referral_consult])
        if @consult.save
          flash[:notice] = "Consult was successfully created."
          get_consults if get_request_referer.eql?("preferences")
        else
          flash[:error] = @consult.errors.full_messages.join("<br/>").html_safe
        end
      }
      format.html{ redirect_to     referral_ic_patients_path(current_ic) }
    end
  end

  def edit
    respond_to do |format|
      format.js{}
      format.html{ redirect_to preferences_ic_bc_reports_path(current_ic) }
    end
  end

  def update
    respond_to do |format|
      format.js{
        if @consult.update_attributes(params[:referral_consult])
          flash[:notice] = "Consult was successfully updated"
          get_consults
        else
          flash[:error] = @consult.errors.full_messages.join("<br/>").html_safe
        end
      }
      format.html{ respond_to preferences_ic_bc_reports_path(current_ic) }
    end
  end

  private

  def get_consults
    @consults = ReferralConsult.created_at_desc.paginate(per_page: 10, page: params[:page])
  end

  def find_consult
    @consult = ReferralConsult.where(id: params[:id]).first
    if @consult.blank?
      flash[:error] = "You don't have access to the requested url."
      js_redirect_to(preferences_ic_bc_reports_path(current_ic)) and return
    end
  end


end




JS.ERB Files

index.js.erb

$(".tab-content").html("<%= j(render partial: 'index') %>");
comp_initialize_ajax_pagination();



new.js.erb


$('.popup_div').html("<%= j(render(partial: 'new')) %>");
$('#consult-popup').modal('show');
initialize_form_validation();



create.js.erb


<% if @consult.errors.present? %>
$(".consult_flash_messages").html("<%= j(render partial: '/shared/flash_messages') %>");
<% else %>
$(".compcore_flash_message").html("<%= j(render partial: '/shared/toastr_flash') %>");
$('#consult-popup').modal('hide');
<% if @consults.present? %>
$(".tab-content").html("<%= j(render partial: 'index') %>");
<% else %>
$("#consult_autocomplete").val("<%= @consult.doctor_name %>").next(".hidden_field_id").val("<%= @consult.id %>");
<% end %>
<% end %>


edit.js.erb

$(".pop_up_div").html("<%= j(render partial: 'edit') %>");
$("#consult-edit-popup").modal("show");
initialize_form_validation();


update.js.erb


<% if @consult.errors.present? %>
$(".consult_flash_messages").html("<%= j(render partial: '/shared/flash_messages') %>");
<% else %>
$("#consult-edit-popup").modal("hide");
$(".compcore_flash_message").html("<%= j(render partial: '/shared/toastr_flash') %>");
$(".tab-content").html("<%= j(render partial: 'index') %>");
<% end %>




HTML.ERB Pages



new.html.erb

<div class="modal fade clinic-popup" id="consult-popup" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">X</span></button>
        <h4 class="modal-title text-center popup-title">Add Consult</h4>
      </div>
      <div class="modal-body text-center clearfix info-box">
        <%= simple_form_for(@consult, url: referral_ic_referral_consults_path(current_ic), remote: true, html: { method: :post } ) do |f| %>
        <%= render partial: "form", locals: {f: f} %>
        <% end %>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div>


form.html.erb

<div class="consult_flash_messages"></div>
<div class="form-group">
  <label class="col-xs-12 col-md-12 control-label">Doctor Name<span>*</span></label>
  <div class="col-xs-12 col-md-12">
    <%= f.text_field :doctor_name, class: "form-control", required: true %>
  </div>
</div>
<div class="form-group">
  <label class="col-xs-12 col-md-12 control-label">Address</label>
  <div class="col-xs-12 col-md-12">
    <%= f.text_field :address, class: "form-control", placeholder: "Address" %>
  </div>
</div>
<div class="form-group">
  <label class="col-xs-12 col-md-12 control-label">Phone</label>
  <div class="col-xs-12 col-md-12">
    <%= f.text_field :phone, class: "form-control" %>
  </div>
</div>
<div class="form-group">
  <label class="col-xs-12 col-md-12 control-label">Fax</label>
  <div class="col-xs-12 col-md-12">
    <%= f.text_field :fax, class: "form-control" %>
  </div>
</div>
<div class="form-group form-actions">
  <div class="col-xs-12">
    <a href="#" class="btn btn-default fl cancel" data-dismiss="modal">Cancel</a>
    <%= f.submit "Submit", class: "btn btn-success fl", data: { disable_with: "Process.." } %>
  </div>
</div>



edit.html.erb


<div class="modal fade clinic-popup" id="consult-edit-popup" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">X</span></button>
        <h4 class="modal-title text-center popup-title">Edit Consult</h4>
      </div>
      <div class="modal-body text-center clearfix info-box">
        <%= simple_form_for(@consult, url: referral_ic_referral_consult_path(current_ic, @consult.id), remote: true, html: { method: :put } ) do |f| %>
        <%= render partial: "form", locals: {f: f} %>
        <% end %>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div>



index.html.erb


<div class="row page-controls">
  <span class="font_size_16">Referral Consult</span>
  <a href="<%= new_referral_ic_referral_consult_path(current_ic) %>" data-remote="true" class="pull-right btn btn-success"><i class="fa fa-plus"></i> Consult</a>
</div><br/>
<div class="row page-controls">
  <table class="table table-striped table-hover">
    <tr>
      <th class="col-sm-3 col-md-3">Doctor Name</th>
      <th class="col-sm-3 col-md-3">Address</th>
      <th class="col-sm-3 col-md-3">Phone</th>
      <th class="col-sm-1 col-md-1">Edit</th>
    </tr>
    <% if @consults.present? %>
    <% @consults.each do |consult| %>
    <tr>
      <td class="col-sm-3 col-md-3"><%= consult.doctor_name %></td>
      <td class="col-sm-3 col-md-3"><%= consult.address %></td>
      <td class="col-sm-3 col-md-3"><%= consult.phone %></td>
      <td class="col-sm-1 col-md-1">
        <a data-remote="true" href="<%= edit_referral_ic_referral_consult_path(current_ic, consult.id) %>">
          <i class="fa fa-pencil-square-o"></i>
        </a>
      </td>
    </tr>
    <% end %>
    <tr>
      <td colspan="4">
        <div align="center"><%= will_paginate @consults, class: "pagination ajax_pagination", inner_window: 2 %></div>
      </td>
    </tr>
    <% else %>
    <tr>
      <td colspan="4">
        <div class="alert alert-warning notice_div mrg_top_10" role="alert">
          Currently there’s no records to display.
        </div>
      </td>
    </tr>
    <% end %>
  </table>
</div>

Comments