Ruby on rails 更新操作似乎什么也没做

Ruby on rails 更新操作似乎什么也没做,ruby-on-rails,forms,controller,parameter-passing,Ruby On Rails,Forms,Controller,Parameter Passing,目前,我有一个externation\u definition\u controller,使用这样的方法 class Edms::OvertimeDefinitionsController < ApplicationController require 'will_paginate/array' layout :compute_layout def new @flag = params[:flag] @employee = Employee.find(pa

目前,我有一个
externation\u definition\u controller
,使用这样的方法

class Edms::OvertimeDefinitionsController < ApplicationController

  require 'will_paginate/array'
  layout :compute_layout


  def new
    @flag = params[:flag]
    @employee = Employee.find(params[:id])
    @overtime = OvertimeDefinition.new
  end

  def create
    @employee = Employee.find(params[:overtime_definition][:employee_id])
    @overtime = OvertimeDefinition.new(params[:overtime_definition])
    if (params[:half_day_extra_duty_hours][:hour].to_s !="" || params[:half_day_extra_duty_hours][:minute].to_s !="")
      @overtime.half_day_extra_duty_hours   = params[:half_day_extra_duty_hours][:hour].to_s + ":" + params[:half_day_extra_duty_hours][:minute].to_s + ":" + "00"
    else
      @overtime.half_day_extra_duty_hours = nil
    end
    if (params[:full_day_extra_duty_hours][:hour].to_s !="" || params[:full_day_extra_duty_hours][:minute].to_s !="")
      @overtime.full_day_extra_duty_hours   = params[:full_day_extra_duty_hours][:hour].to_s + ":" + params[:full_day_extra_duty_hours][:minute].to_s + ":" + "00"
    else
      @overtime.full_day_extra_duty_hours = nil
    end
    if @overtime.save
      flash[:notice] = "Overtime Successfully Created for #{@employee.name}"
      redirect_to :action => 'search_overtime'
    end
  end

  def edit
    @flag = params[:flag]
    @overtime = OvertimeDefinition.find(params[:id])
    @employee = Employee.find(params[:employee_id])
  end

  def update
    @employee = Employee.find(params[:id])
    @overtime = OvertimeDefinition.find(params[:id])
    if @overtime.update_attributes(params[:overtime_definition])
      flash[:notice] = "Overtime Successfully Updated for #{@employee.name}"
      redirect_to :action => 'search_overtime'
    else
      render :action => 'edit',:flag=>params[:flag]
    end
  end

  def search_overtime
    @overtimes = OvertimeDefinition.all
    @departments = @site.is_central ? {} : @site.departments
    @designations = @site.is_central ? {} : @site.designations
    @active_profile = @site.is_central ? Employee.get_all_employees.paginate(:page => params[:page]) : @site.employees.get_all_employees.paginate(:page => params[:page]) 
    if request.xml_http_request?
      render :update do |page|
      page['search_result_div'].replace_html :partial => "search_overtime_employee_list"
    end
  end
end
<%= hidden_field 'overtime_definition','employee_id',:value=>@employee.id %>
@employee = Employee.find(params[:employee_id])
<%= form_tag :action => 'update',:id => @overtime.id,:employee_id => params[:employee_id],:flag=> params[:flag] %>
\u form.rhtml

<%= form_tag :action => 'update',:id => @overtime.id,:employee_id => @employee.id,:flag=> params[:flag] %>
<%= render :partial =>'form' %>
<center>
  <%= submit_tag "Update",:onclick=>"return validate()",:class=>"buttons" %>
</center>
<%= link_to "Back" ,:action => "search_overtime" %>
<table cellspacing="5">
  <tr>
    <td><b>Employee Code</b></td>
    <%= hidden_field 'overtime_definition','employee_id',:value=>params[:id] %>
    <td><%= @employee.employeeid %></td>
    <td><b>Employee Name</b></td>
    <td><%= @employee.personnel.display_full_name %></td>
  </tr>
  <tr>
    <td><b>Department</b></td>
    <td><%=  @employee.department ? @employee.department.name : "" %></td>
    <td><b>Designation</b></td>
    <td><%= @employee.designation ? @employee.designation.name : "" %></td>
    <td><b>Location</b></td>
    <td><%= @employee.location.name%></td>
  </tr>
</table>

</br>
<fieldset>
  <table cellspacing="5">
    <%= form_for :overtime_definition, :builder => LabelFormBuilder do |od| %>
      <tr>
        <td>
          <label for="half_day_extra_duty_hours">
            Half Day Extra Duty Hours
          </label>
        </td>
        <td class ="datefamily">
          <%= select_time(@overtime.half_day_extra_duty_hours, {:include_blank => true, :time_separator => ":",:prefix => "half_day_extra_duty_hours"})%>
        </td>
      </tr>
      <tr>
        <td>
          <label for="full_day_extra_duty_hours">
            Full Day Extra Duty Hours
          </label>
        </td>
        <td class ="datefamily">
          <%= select_time(@overtime.full_day_extra_duty_hours, {:include_blank => true, :time_separator => ":",:prefix => "full_day_extra_duty_hours"})%>
        </td>
      </tr>
      <tr>
        <%= od.sr_check_box :is_salary_basis, {}, true, false, :label => "Is Salary Basis"%>
      </tr>
      <tr>
        <%= od.sr_check_box :is_fixed_amount, {}, true, false, :label => "Is Fixed Amount"%>
        <td colspan="2" id="ov_hm" style="display: none">
          Half Day Amount
          <%= od.text_field :half_day_amount, :onkeypress => "return numbersonly(event)", :style => "width:40px" %>
        </td>
        <td colspan="2" id="ov_fm" style="display: none">
          Full Day Amount
          <%= od.text_field :full_day_amount, :onkeypress => "return numbersonly(event)", :style => "width:40px" %>
        </td>
      </tr>
    <% end %>
  </table>
</fieldset>
<table cellspacing="0" cellpadding="0" width="100%" id='type'>
  <tr class="tblebluehead">
    <th>
      SI.NO
    </th>
    <th>
      Code
    </th>
    <th>
      Name
    </th>
    <th>
      Department
    </th>
    <th>
      Designation
    </th>
    <th colspan="3">
      Functions
    </th>
  </tr>
  <% count=0 %><% i=0 %>
  <% for employee in @active_profile %>
    <% if i%2==0 %>
      <tr class="tablerow1">
    <% else %>
      <tr class="tablerow2">
    <%end %><% i=i+1 %>
    <td>
      <%= count=count+1 %>
    </td>
    <td>
      <%= employee.employeeid %>
    </td>
    <td>
      <%=h employee.name %>
      <%resigned = EmployeeSeparationDetail.find(:first, :conditions => ["employee_id =? and status ='Active' and approval_status ='Approved'", employee.id])%>               
      <% if resigned %>
        <span style="color:red"> (R)</span>
      <% end %>
    </td>
    <td>
      <%=h employee.department ? employee.department.name : ""%>
    </td>
    <td>
      <%=h employee.designation ? employee.designation.name : "" %>
    </td>
    <td>
      <%=link_to "Calculation" ,:action => "new",:id=>employee.id,:flag=>"Calculation" %>
    </td>
    <% for overtime in @overtimes %>
      <% if overtime.employee_id == employee.id%>
        <td>
          <%= link_to "Re-Calculate",:action => "edit",:id=>overtime.id,:employee_id => employee.id,:flag=>"Re-Calculate" %>
        </td>
      <% end %> 
    <% end %>
    </tr>
  <% end %>
</table>
<b> <%= will_paginate @active_profile,:next_label => "&gt;&gt;" ,:prev_label => "&lt;&lt;"%></b>
更新1

更改了\u form.rhtml中的隐藏字段,如下所示

class Edms::OvertimeDefinitionsController < ApplicationController

  require 'will_paginate/array'
  layout :compute_layout


  def new
    @flag = params[:flag]
    @employee = Employee.find(params[:id])
    @overtime = OvertimeDefinition.new
  end

  def create
    @employee = Employee.find(params[:overtime_definition][:employee_id])
    @overtime = OvertimeDefinition.new(params[:overtime_definition])
    if (params[:half_day_extra_duty_hours][:hour].to_s !="" || params[:half_day_extra_duty_hours][:minute].to_s !="")
      @overtime.half_day_extra_duty_hours   = params[:half_day_extra_duty_hours][:hour].to_s + ":" + params[:half_day_extra_duty_hours][:minute].to_s + ":" + "00"
    else
      @overtime.half_day_extra_duty_hours = nil
    end
    if (params[:full_day_extra_duty_hours][:hour].to_s !="" || params[:full_day_extra_duty_hours][:minute].to_s !="")
      @overtime.full_day_extra_duty_hours   = params[:full_day_extra_duty_hours][:hour].to_s + ":" + params[:full_day_extra_duty_hours][:minute].to_s + ":" + "00"
    else
      @overtime.full_day_extra_duty_hours = nil
    end
    if @overtime.save
      flash[:notice] = "Overtime Successfully Created for #{@employee.name}"
      redirect_to :action => 'search_overtime'
    end
  end

  def edit
    @flag = params[:flag]
    @overtime = OvertimeDefinition.find(params[:id])
    @employee = Employee.find(params[:employee_id])
  end

  def update
    @employee = Employee.find(params[:id])
    @overtime = OvertimeDefinition.find(params[:id])
    if @overtime.update_attributes(params[:overtime_definition])
      flash[:notice] = "Overtime Successfully Updated for #{@employee.name}"
      redirect_to :action => 'search_overtime'
    else
      render :action => 'edit',:flag=>params[:flag]
    end
  end

  def search_overtime
    @overtimes = OvertimeDefinition.all
    @departments = @site.is_central ? {} : @site.departments
    @designations = @site.is_central ? {} : @site.designations
    @active_profile = @site.is_central ? Employee.get_all_employees.paginate(:page => params[:page]) : @site.employees.get_all_employees.paginate(:page => params[:page]) 
    if request.xml_http_request?
      render :update do |page|
      page['search_result_div'].replace_html :partial => "search_overtime_employee_list"
    end
  end
end
<%= hidden_field 'overtime_definition','employee_id',:value=>@employee.id %>
@employee = Employee.find(params[:employee_id])
<%= form_tag :action => 'update',:id => @overtime.id,:employee_id => params[:employee_id],:flag=> params[:flag] %>
更新3

更改了edit.rhtml中的表单标记参数,如下所示

class Edms::OvertimeDefinitionsController < ApplicationController

  require 'will_paginate/array'
  layout :compute_layout


  def new
    @flag = params[:flag]
    @employee = Employee.find(params[:id])
    @overtime = OvertimeDefinition.new
  end

  def create
    @employee = Employee.find(params[:overtime_definition][:employee_id])
    @overtime = OvertimeDefinition.new(params[:overtime_definition])
    if (params[:half_day_extra_duty_hours][:hour].to_s !="" || params[:half_day_extra_duty_hours][:minute].to_s !="")
      @overtime.half_day_extra_duty_hours   = params[:half_day_extra_duty_hours][:hour].to_s + ":" + params[:half_day_extra_duty_hours][:minute].to_s + ":" + "00"
    else
      @overtime.half_day_extra_duty_hours = nil
    end
    if (params[:full_day_extra_duty_hours][:hour].to_s !="" || params[:full_day_extra_duty_hours][:minute].to_s !="")
      @overtime.full_day_extra_duty_hours   = params[:full_day_extra_duty_hours][:hour].to_s + ":" + params[:full_day_extra_duty_hours][:minute].to_s + ":" + "00"
    else
      @overtime.full_day_extra_duty_hours = nil
    end
    if @overtime.save
      flash[:notice] = "Overtime Successfully Created for #{@employee.name}"
      redirect_to :action => 'search_overtime'
    end
  end

  def edit
    @flag = params[:flag]
    @overtime = OvertimeDefinition.find(params[:id])
    @employee = Employee.find(params[:employee_id])
  end

  def update
    @employee = Employee.find(params[:id])
    @overtime = OvertimeDefinition.find(params[:id])
    if @overtime.update_attributes(params[:overtime_definition])
      flash[:notice] = "Overtime Successfully Updated for #{@employee.name}"
      redirect_to :action => 'search_overtime'
    else
      render :action => 'edit',:flag=>params[:flag]
    end
  end

  def search_overtime
    @overtimes = OvertimeDefinition.all
    @departments = @site.is_central ? {} : @site.departments
    @designations = @site.is_central ? {} : @site.designations
    @active_profile = @site.is_central ? Employee.get_all_employees.paginate(:page => params[:page]) : @site.employees.get_all_employees.paginate(:page => params[:page]) 
    if request.xml_http_request?
      render :update do |page|
      page['search_result_div'].replace_html :partial => "search_overtime_employee_list"
    end
  end
end
<%= hidden_field 'overtime_definition','employee_id',:value=>@employee.id %>
@employee = Employee.find(params[:employee_id])
<%= form_tag :action => 'update',:id => @overtime.id,:employee_id => params[:employee_id],:flag=> params[:flag] %>
'update',:id=>@加班.id,:employee\u id=>params[:employee\u id],:flag=>params[:flag]>
现在,更新操作后,它不会将
employee\u id
值转换为
加班定义的
id
,但这些值不会更新

我怀疑控制器中的
编辑表单
参数
编辑/更新
操作可能导致此问题。无法找出原因

非常感谢您的帮助

def update
    @employee = Employee.find(params[:id])
    @overtime = OvertimeDefinition.find(params[:id])
那么,您的两个模型填充了完全相同的数据


您可能会更好地使用:

def update
    @employee = Employee.find(params[:id])
    @overtime = OvertimeDefinition.find(params[:employee_id])

如果您发布日志中的参数散列,我会更好地提供帮助,但这是您问题的核心,请使用
Graeme McLean
的信息也嗨,Suresh,请使用两个空格缩进来缩进您的代码。如果没有缩进,阅读起来会非常困难@RyanBigg道歉!我不知道如何像你说的那样编辑。你新操作中的代码应该用两个空格缩进。编辑操作中的代码应该缩进两个空格。。。等等。@RyanBigg你是说控制器中所有动作的行之间有两个空格?抱歉!你能告诉我如何查看我的日志吗?不需要道歉!日志位于
log/[[environment]]中。日志
。如果单击
development.log
,您将能够按时间顺序查看请求;允许您向我们显示params hash:)并查看我之前的问题,该问题与此相关。您可能会获得有关params的一些信息。使用参数更新了我的问题。进一步更新,让您了解更多信息。请查看。