以Rails形式动态更新字段值

时间:2015-03-25 05:17:35

标签: ruby-on-rails ruby forms

我的ruby应用程序中有一个表单,其中有一个字段total_hours_spent和不同的字段,用于分解各种活动,如design_hours,analysis_hours,coding_hours等。我正在寻找一种机制,只要所有分解时间字段填写后,总小时字段应动态更新。可悲的是我不知道Ajax或Jquery。我知道javascript但不知道如何使用rails。

所以我现在正在做的是,一旦用户提交表单,就会计算这些值,然后将其存储在数据库中。有没有办法只使用rails来动态显示字段值?

下面是表单代码:我想添加两个添加一个字段total_hors,它将自动填充值build_hours + test_hours + analysis_hours以及其他几个字段。如何做到这一点?

<%= nested_form_for(@rfsestimation) do |f| %>

<% if @rfsestimation.errors.any? %>
      <h2> These errors prohibited the RFS from being saved </h2>
<font color="red">
        <ul>
          <% @rfsestimation.errors.full_messages.each do |msg| %>
          <li><%= msg %> </li>
         <% end %>
    <% end %>
        </ul>
</font>
<div class = "col-md-6">
 <h4>IRIS AME RFS Estimation Summary Sheet</h4>

<table class = "table">
<tr> <td> RFS Number </td>
<td> <%= f.text_field :number, :placeholder => "Enter RFS Number" %> </td>
</tr>
<tr> <td> RFS Name </td>
<td> <%= f.text_field :name, :placeholder => "Enter RFS Name" %> </td>
</tr>
<tr> <td> Date of Estimation </td>
<td> <%= f.date_select :date_of_estimation, :placeholder => "Enter Date of Estimation" %> </td>
</tr>
<tr> <td> Request Type </td>
<td> <%= f.select :request_type_id , Rfsestimation.request_types.map { |k, v| [k.humanize, v] } %> </td>
</tr>
<tr> <td>Band</td>
<td> <%= f.select :band_id , Rfsestimation.band_types.map { |k, v| [k.humanize, v] } %> </td>
</tr>
</table>
<br/>

<table class = "table">
  <thead>
    <tr>
   <td>Task</td>
   <td>Effort(Hours)</td>
   <td>Comments</td>
    </tr>
  </thead>
  <tbody>
  <tr>
    <td>
      Analysis / Requirements Gathering
    </td>
    <td>
<%= f.fields_for :rfstaskset do |rfs_task| %>
      <%= rfs_task.number_field :analysis_hours %>
    </td>
     <td>
       <%= rfs_task.text_area :analysis_comments, :size=> "40x1" %>
    </td>
  </tr>  

    <tr>
    <td>
      Design
    </td>
    <td>
          <%= rfs_task.number_field :design_hours %>
    </td>
     <td>
       <%= rfs_task.text_area :design_comments, :size=> "40x1" %>
    </td>
  </tr>  

     <tr>
    <td>
      Build
    </td>
    <td>
      <%= rfs_task.number_field :build_hours %>
    </td>
     <td>
       <%= rfs_task.text_area :build_comments, :size=> "40x1" %>
    </td>
  </tr> 
    <tr>
    <td>
      Test
    </td>
    <td>
      <%= rfs_task.number_field :test_hours %>
    </td>
     <td>
       <%= rfs_task.text_area :test_comments, :size=> "40x1" %>
    </td>
  </tr> 
     <tr>
    <td>
      UAT Support
    </td>
    <td>
      <%= rfs_task.number_field :UATSupport_hours %>
    </td>
     <td>
       <%= rfs_task.text_area :UATSupport_comments, :size=> "40x1" %>
    </td>
  </tr> 
     <tr>
    <td>
      Deployment Support and
      Deployment instruction document
    </td>
    <td>
      <%= rfs_task.number_field :DepSupport_hours %>
    </td>
     <td>
       <%= rfs_task.text_area :DepSupport_comments, :size=> "40x1" %>
    </td>
  </tr> 

    <tr>
    <td>
      contingency
    </td>
    <td>
      <%= rfs_task.text_field :hours_per_day, :value => "10%", :disabled => "true"  %>
    </td>
     <td>
       <%= rfs_task.text_area :contingency_comments, :size=> "40x1" %>
    </td>
  </tr> 



  </tbody>
    <% end %>

  </table>
  <%= f.submit "Create RFS", class: "btn btn-success"%>
  </div>
  <div class = "col-md-6">
  <table class="table">
  <br/><br/>
  <tr>
  <td> Hours/day : </td> <td> <%= f.text_field :hours_per_day, :placeholder => "Enter hours per day", :value => "8.8", :disabled => "false" %> </td>
  </tr>
  <tr>
  <td> Estimated Start Date : </td> <td> <%= f.date_select :estimated_start_date %> </td>
  </tr>
  <tr>
  <td> Estimated End Date : </td> <td> <%= f.date_select :estimated_end_date %> </td>
  </tr>


  </table> <br/> <br/> <br/> <br/><br/> <br/><br/> 
  <%= f.fields_for :rfsnote do |rfs_note| %>
  <%= rfs_note.text_area :notes, :size=> "60x20", :placeholder => "Enter RFS Notes" %>
  <% end %>
  </div>

  <% end %>

3 个答案:

答案 0 :(得分:2)

您可以使用before_save回调 例如

before_save :update_total_houres

 def update_total_houres
   total_hours_spent = design_hours + analysis_hours + coding_hour
 end

也使用JavaScript

$(document).ready(function(){

$('.hours').change(function() {
    update_total_hours();
});
});

function update_total_hours()
{
var total = 0; 

var analysis_hours = $(".analysis_hours").val();    
var design_hours = $(".design_hours").val();    
var coding_hour = $(".coding_hour").val();  
total = sum all   
//just update the total to sum  
$('.total').text(total);
} 

答案 1 :(得分:0)

您可以在模型中进行before_create OR before_save回调,它将自动存储在数据库中,如:

before_create :calculate_total_hours

def calculate_total_hours
  total_hours_spent = design_hours, analysis_hours, coding_hours
end

你也可以通过jquery做,但在模型中做一些事情将是强大的业务逻辑

答案 2 :(得分:0)

因此,如果您在数据库中具有所需的值,则可以直接将其用作实例变量并在视图中显示它...例如

def create
 #here you create/save the required data in db
 @user=User.new(params[:new])
  if @user.save
   #get the values you need
   @time_used=@user.time_used
   @build_hrs=@user.build_hrs
   ...
   ...
   #and so on
   #all these values will be available in the view directly
  redirect_to users_show_url
 else
  render :new
 end 
end##method ends