无法获得编辑功能工作

时间:2014-04-07 12:33:25

标签: ruby-on-rails controller

我有两个模型EmployeeOvertime Definition关联设置如下

员工

class Employee < ActiveRecord::Base

has_many :overtime_definitions

加班定义

class OvertimeDefinition < ActiveRecord::Base

belongs_to :employee

我为员工创建了一个加班定义,一切看起来都很好。但是我在为员工编辑时却遇到了麻烦。

overtime_definitions__controller:

    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_id])
    @employee = Employee.find(params[: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

在编辑方法

中尝试使用这些
  1. @overtime = OvertimeDefinition.find(params[:id][:employee_id])

    #gives me can't convert Symbol into Integer错误。

  2. @overtime = OvertimeDefinition.find(params[:id])

    #gives me Couldn't find OvertimeDefinition with ID=1353 error.Actually 1353是该员工的ID。

    3。@overtime = OvertimeDefinition.find(params[:employee_id])

    #gives me couldn't find OvertimeDefinition without an ID错误。

  3. 我的_search_overtime_employee_list包含用于新操作和编辑操作的这些链接

    <%=link_to "Calculation" ,:action => "new",:id=>employee.id, :flag=>"Calculation" %>
    
    <%= link_to "Re-Calculate",:action => "edit",:id=>employee.id,:flag=>"Re-Calculate" %>
    

    new.rhtml

    <%= form_tag :action => 'create' do %>
    <%= render :partial =>'form'%>
    <center>
        <%= submit_tag "Save",:onclick=>"return validate()",:class=>"buttons"%>
    </center>
    <% end %>
    <%= link_to "Back" ,:action => "search_overtime" %>
    

    edit.rhtml

    <%= form_tag :action => 'update', :id=>@employee.id,:flag=> params[:flag],:value=>params[:id] %>
    <%= render :partial =>'form'%>
    <center>
        <%= submit_tag "Update",:onclick=>"return validate()",:class=>"buttons"%>
    </center>
    <%= link_to "Back" ,:action => "search_overtime" %>
    

    与_form.rhtml

       员工详细信息

    <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>
    

    我完全失去了这个编辑动作的工作。非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

您当前的edit链接是:

<%= link_to "Re-Calculate",:action => "edit",:id=>employee.id,:flag=>"Re-Calculate" %>

在您的编辑操作中:

  

@overtime = OvertimeDefinition.find(params [:id] [:employee_id])##让我无法将符号转换为整数错误。

根据修改链接,您可以直接在查询参数中传递:id,您可以将其作为params[:id]进行访问。你的params哈希中没有params[:id][:employee_id]所以当你说params[:id][:employee_id] Ruby试图将:employee_id转换为一个符号整数时。因此,错误。

我认为你应该从你的链接传递:id中的OvertimeDefinition记录的id。并以

的形式访问它
@overtime = OvertimeDefinition.find(params[:id])

在Controller的行动中。

  

@overtime = OvertimeDefinition.find(params [:id])##给了我不能   找到ID = 1353错误的OvertimeDefinition。实际上1353是id   那个员工。

这是因为您在employee id中传递params[:id],所以显然这不起作用。你需要在这里传递OvertimeDefinition id。

  

@overtime = OvertimeDefinition.find(params [:employee_id])##给了我   没有ID错误,找不到OvertimeDefinition。

您未在:employee_id链接中的查询参数中传递任何edit。因此,params [:employee_id]将为nil并且find方法失败,因为您没有向其传递任何ID。

解决方案:

更新您的edit link,如下所示:

<%= link_to "Re-Calculate",:action => "edit",:id=> @overtimedefinition.id , :employee_id => employee.id,:flag=>"Re-Calculate" %> 

@overtimedefinition.id替换为适当的OvertimeDefinition记录ID。由于您还没有共享代码,我不知道OvertimeDefinition变量的名称。

将您的edit操作更新为:

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