mercredi 27 juin 2018

How to auto Update fields in rails

I have a general settings table, parents table, children table and fundings table. Children table has funding_id. each child can have multiple funding applications. In the funding table there is a field status which can be changed only by admin. Once the admin approves the application, I want the amount requested to be subtracted from the amount_remaining field in the children table.The default amount_remaining is the amount_current_year in the general settings. How can I achieve this. Kindly help me.

Funding table schema

 create_table "fundings", force: :cascade do |t|
    t.string "type_of_activity"
    t.string "season"
    t.text "activity_details"
    t.string "name_of_organisation"
    t.date "activity_start_date"
    t.string "number_of_weeks"
    t.string "days_per_week"
    t.string "hours_per_day"
    t.integer "program_registration_cost"
    t.string "family_contribution"
    t.integer "other_funds"
    t.string "other_fund_provider"
    t.integer "amount_requested"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "child_id"
    t.string "status"
    t.date "date_submitted"
  end

General setting scheme

  create_table "generalsettings", force: :cascade do |t|
    t.integer "amount_current_year"
    t.integer "amount_next_year"
    t.date "current_year"
    t.date "next_year"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

Children table schema

create_table "children", force: :cascade do |t|
    t.string "firstname"
    t.string "lastname"
    t.date "dateofbirth"
    t.string "gender"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "parent_id"
    t.integer "amount_remaining"
  end

_form.html.erb (funding application form)

<div class='row'>
  <div class= 'col-xs-12'>
    <%= form_for([@parent, @child, @funding], :html => {class: "form-horizontal",role: "form"}) do |form| %>
      <div class = "form-group">
        <div class="control-label col-sm-2">
          <%= form.label :type_of_activity, class: "required" %>
        </div>
        <div class="col-sm-8">
          <%= form.select :type_of_activity, ['Swimming', 'Soccer', 'Cricket', 'Basket Ball'], required: true %>
        </div>
      </div>
      <div class = "form-group">
        <div class="control-label col-sm-2">
          <%= form.label :activity_details, class: "required" %>
        </div>
        <div class="col-sm-8">
          <%= form.text_area :activity_details, required: true %>
        </div>
      </div>
      <div class = "form-group">
        <div class="control-label col-sm-2">
          <%= form.label :name_of_organisation, class: "required" %>
        </div>
        <div class="col-sm-8">
          <%= form.select :name_of_organisation, ['BCCI', 'IPL', 'Sahara', 'Not listed'], class: "form-control", autofocus:true, required: true %>
        </div>
      </div>
      <div class = "form-group">
        <div class="control-label col-sm-2">
          <%= form.label :activity_start_date, class: "required" %>
        </div>
        <div class="col-sm-8">
          <%= form.date_field :activity_start_date, min: Date.today, required: true %>
        </div>
      </div>
      <div class = "form-group">
        <div class="control-label col-sm-2">
          <%= form.label :program_registration_cost, class: "required" %>
        </div>
        <div class="col-sm-8">
          <%= form.text_field :program_registration_cost, required: true %>
        </div>
      </div>
      <div class = "form-group">
        <div class="control-label col-sm-2">
          <%= form.label :amount_requested, class: "required" %>
        </div>
        <div class="col-sm-8">
          <%= form.text_field :amount_requested, required: true %>
        </div>
      </div> 
      <div class = "form-group">
        <div class="control-label col-sm-2">
          <%= form.label :date_submitted, class: "required" %>
        </div>
        <div class="col-sm-8">
          <%= form.date_field :date_submitted, value: Date.today, readonly:true, required: true %>
        </div>
      </div>
      <div class = "form-group">
        <div class="control-label col-sm-2">
          <%= form.label :status %>
        </div>

      <% if current_user.admin? %>
        <div class="col-sm-8">
          <%= form.select :status,['Pending', 'Approved', 'Declined'], class: "form-control" %>
        </div>
      <% else %>
        <div class="col-sm-8">
          <%= form.select :status,['Pending', 'Approved', 'Declined'], {class: "form-control"}, {:disabled => true} %>
        </div>
      <% end %>

      </div> 
      <div class="form-group">
        <div class="col-sm-10">
          <%= form.submit "Apply", class: 'btn btn-primary btn-lg' %>
        </div>
      </div>
    </div>
  </div>

    <% end %>

Aucun commentaire:

Enregistrer un commentaire