# Passing a calculated value in a hidden\_field

**URL:** <https://www.ruby-forum.com/t/passing-a-calculated-value-in-a-hidden-field/109544>\
**Category:** Rails\
**Created:** [July 16, 2007, 6:28pm UTC](https://www.ruby-forum.com/t/passing-a-calculated-value-in-a-hidden-field/109544 "2007-07-16T18:28:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ebrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@ebrad](https://www.ruby-forum.com/u/ebrad)\
**Post date:** [July 16, 2007, 6:28pm UTC](https://www.ruby-forum.com/t/passing-a-calculated-value-in-a-hidden-field/109544/1 "2007-07-16T18:28:40Z")

</div>

I have a calculated number that I wish to use to send as a value in a  
hidden\_field in a form. This started out as a question, but I figured  
it out so I’ll post it anyway as a FYI for Ruby N.s like me or if  
anyone can think of a smarter way.

A few points:  
\*The value is calculated in the controller and assigned to an instance  
variable  
\*The ‘TimeOff’ model is in a has\_many relationship with the ‘Employee’  
model (An Employee can take many days off).  
\*The resulting value is being passed as a hidden\_field to populate the  
table ‘time\_offs’

Either of these work:

```
 <%= hidden_field "time_off", "hours_left", :value =>

```

“#{pto\_accrued}” %\> \<%= hidden\_field “time\_off”,  
“hours\_left”, :value =\> @pto\_accrued %\>

Below is my code:

# Employee Model

####################################  
class Employee \< ActiveRecord::Base  
has\_many :time\_offs  
end

### 

# TimeOff Model

####################################  
class TimeOff \< ActiveRecord::Base  
belongs\_to :employee  
end

### 

# Employee Controller

####################################  
class EmployeesController \< ApplicationController  
…  
def timesheets\_and\_time\_off

```
# Employee parameters being passed
@employee = Employee.find(params[:id])

# The block below calculates Time Off for an employee
start_date = (@employee.hire_date).to_time
end_date = Time.now
days = ((end_date - start_date)/(3600*24))
accumulated_pto = ((days/14)*1.54)

# Final Value for my View
@pto_accrued = accumulated_pto

```

end

# This is the ‘create’ method for Time Offs that belong to an Employee

def create\_pto  
Employee.find(params[:id]).time\_offs.create(params[:time\_off])  
flash[:notice] = “PTO Was Successfully Added”  
redirect\_to :action =\> “timesheets\_and\_time\_off”, :id =\> params[:id]  
end  
…  
end

#### 

# View (timesheets\_and\_time\_off.rhtml)

# If there are no Time Offs for the employee, it will only show a

‘Calculate’ button, which calculates from the Employee.hire\_date.  
Once it is calculated the total will appear in a table.  
######################################  
…

# Paid Time Off
\<% if @employee.time\_offs.blank? %\> 

No time off has been recorded yet. Click 'Calculate' to generate a total.

\<% form\_tag :controller =\> ‘employees’, :action =\> ‘create\_pto’, :id  
=\> @employee do %\>  
\<%= hidden\_field “time\_off”, “hours\_left”, #{pto\_accrued} %\>

  
\<%= image\_submit\_tag(“calculate”) %\>  
\<% end %\>

\<% else %\>

\<% for time\_off in @employee.time\_offs %\>

```
<td><%= time_off.created_at.to_s(:long) %></td>
<td><% # comment.created_at.to_s(:long) %></td>
<td style="color: red;"><%= time_off.hours_taken %></td>
<td><% # comment.comment_type %></td>
</tr>

```

\<% end %\>

| Date | Hours Before | Hours Taken | Hours Left |
| --- | --- | --- | --- |
|
| |

## Add PTO
 \<% form\_tag :controller =\> 'employees', :action =\> 'pto', :id =\> @employee do %\> 

Hours Taken (\<%= @pto\_accrued %\> hrs available)  
 \<%= text\_field "time\_off", "hours\_taken" %\>

\<%= image\_submit\_tag(“save”) %\>  
\<% end %\>  
\<% end %\>

.... #####
