I have two tables salary_structures and payheads. Below are my table structures:
create_table "payheads", :force => true do |t|
t.integer "company_id", :null => false
t.integer "defined_by", :null => false
t.string "payhead_type"
t.string "payhead_name"
t.string "under"
t.string "affect_net_salary"
t.string "name_appear_in_payslip"
t.string "use_of_gratuity"
t.datetime "created_at"
t.datetime "updated_at"
end
and:
create_table "salary_structures", :force => true do |t|
t.integer "company_id", :null => false
t.integer "for_employee"
t.integer "created_by"
t.date "effective_from_date"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "payhead_id"
t.decimal "amount", :precision => 18, :scale => 2, :default => 0.0
end
As per my table structure i have payhead_id in my salary_structures table and in my payheads table i have four payheads (basic, Pf etc.). Now when a admin want to define salary_structure of an employee he has to fix amount for every payhead, as per my design i want to save all payheads for an employee for a single salary_structure at once.But here is the problem is for one salary_structure id how can i save more than one payhead’s. Below are my view form:
<%= form_for(@salary_structure) do |f| %>
<%= render 'shared/form_error', :object => @salary_structure %>
<div class="field">
<%= f.label :for_employee, :class => "required" %><br />
<%= collection_select(:salary_structure, :for_employee, @users, :id, :first_name,:autofocus =>"autofocus", :prompt => true) %>
</div>
<div class="column width3">
<div class="field">
<%= f.label :effective_from_date, :class => "required" %><br />
<%= f.text_field :effective_from_date %>
</div>
</div>
<div class="column width6 first">
<table class=" display stylized full" id="salary_structure_report" style="">
<thead>
<tr>
<th width="80%"><label class="required">Pay Head</label></th>
<th width = "20%"><label class="required">Amount</label></th>
</tr>
</thead>
<tbody>
<% for ph in @payheads %>
<tr>
<td width = "80%">
<%= f.hidden_field "payhead_id", ph.id %>
<%= ph.payhead_name %> </td>
<td width = "20%" ><%= f.text_field :amount %></td>
</tr>
<% end %>
</tbody>
<tfoot>
<tr>
<td class="ta-right" width = "80%">Total</td>
<td class="ta-left" width = "20%"><span class="WebRupee">Rs</span><span id = "total">00.00</span></td>
</tr>
</tfoot>
</table>
</div><br />
<div class = "column width3 first">
<button type="submit" class="btn btn-green big"><img src="/images/add.png" class="icon" /> Save</button>
<%= link_to 'Cancel', salary_structures_path, :class=>"btn btn-gray bg"%>
</div>
</div>
<% end %>
When i tried to save it normally, it does not save payhead id in salary_structures table
, should i have to do something in models. Any help,would be apologize thanks in advance.
Hi friends i have found new way to solve it by using an other table named “line_item” and used a “.build” in my controller and it some how working. In my form i have used nested form.