Advertisement
dmontal2

rails_pagination

Aug 26th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 2.24 KB | None | 0 0
  1. #patients_controller.rb
  2.  
  3. class PatientsController < ApplicationController
  4.   before_action :set_patient, only: [:show, :edit, :update, :destroy]
  5.  
  6.   # GET /patients
  7.   def index
  8.     @patients = Patient.all
  9.   end
  10.  
  11.   # GET /patients/1
  12.   def show
  13.   end
  14.  
  15.   # GET /patients/new
  16.   def new
  17.     @patient = Patient.new
  18.   end
  19.  
  20.   # GET /patients/1/edit
  21.   def edit
  22.   end
  23.  
  24.   # POST /patients
  25.   def create
  26.     @patient = Patient.new(patient_params)
  27.  
  28.     if @patient.save
  29.       redirect_to @patient, notice: 'Patient was successfully created.'
  30.     else
  31.       render :new
  32.     end
  33.   end
  34.  
  35.   # PATCH/PUT /patients/1
  36.   def update
  37.     if @patient.update(patient_params)
  38.       redirect_to @patient, notice: 'Patient was successfully updated.'
  39.     else
  40.       render :edit
  41.     end
  42.   end
  43.  
  44.   # DELETE /patients/1
  45.   def destroy
  46.     @patient.destroy
  47.     redirect_to patients_url, notice: 'Patient was successfully destroyed.'
  48.   end
  49.  
  50.   private
  51.     # Use callbacks to share common setup or constraints between actions.
  52.     def set_patient
  53.       @patient = Patient.find(params[:id])
  54.     end
  55.  
  56.     # Only allow a trusted parameter "white list" through.
  57.     def patient_params
  58.       params.require(:patient).permit(:fname, :lname, :mname, :dob, :medactID)
  59.     end
  60.    
  61.     #pagination
  62.     def index
  63.     @patients = Patient.page(params[:page])
  64.     end
  65.    
  66. end
  67.  
  68. #index.html.erb
  69.  
  70. <p id="notice"><%= notice %></p>
  71.  
  72. <h1>Patients</h1>
  73.  
  74. <table>
  75.   <thead>
  76.     <tr>
  77.       <th>Fname</th>
  78.       <th>Lname</th>
  79.       <th>Mname</th>
  80.       <th>Dob</th>
  81.       <th>Medactid</th>
  82.       <th colspan="3"></th>
  83.     </tr>
  84.   </thead>
  85.  
  86.   <tbody>
  87.     <% @patients.each do |patient| %>
  88.       <tr>
  89.         <td><%= patient.fname %></td>
  90.         <td><%= patient.lname %></td>
  91.         <td><%= patient.mname %></td>
  92.         <td class="dob"><%= patient.dob %></td>
  93.         <td><%= patient.id %></td>
  94.         <td><%= link_to 'Show', patient %></td>
  95.         <td><%= link_to 'Edit', edit_patient_path(patient) %></td>
  96.         <td><%= link_to 'Destroy', patient, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  97.       </tr>
  98.     <% end %>
  99.   </tbody>
  100. </table>
  101.  
  102. <br>
  103.  
  104. <%= link_to 'New Patient', new_patient_path %>
  105.  
  106.  
  107. <div><%= paginate @patients%></div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement