无法获得编辑功能工作 [英] Cant get the Edit function Working

查看:130
本文介绍了无法获得编辑功能工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型 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] [:full_day_extra_duty_hours] [:full_day_extra_duty_hours] [:full_day_extra_duty_hours] [:分] .to_s! .to_s +:+00
else
@ overtime.full_day_extra_duty_hours = nil
end
如果@ overtime.save
flash [:notice] =逾时成功创建#{@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 [:id])
@overtime = OvertimeDefinition.find(params [:id])
if @ overtime.update_attributes(params [:overtime_definition])
flash [:notice] =已成功更新#{@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 无法将Symbol转换为整数错误。


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



    #gives me 找不到OvertimeDefinition ID = 1353 错误。实际上1353是该员工的ID。



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



    #gives me 找不到没有ID 错误的OvertimeDefinition。


我的_search_overtime_employee_list有新的和编辑动作的这些链接

 <%= link_to Calculation,:action => new,:id => employee.id,:flag =>Calculation%> 

<%= link_to重新计算,:action => edit,:id => employee.id,:flag =>重新计算%>

new.rhtml

 <%= form_tag:action => 'create'do%> 
<%= render:partial =>'form'%>
< center>
<%= submit_tagSave,:onclick =>return validate(),:class =>buttons%&
< / center>
<%end%>
<%= link_to返回,:action => search_overtime%>

edit.rhtml

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

_form.rhtml




员工详细信息

 < table cellspacing =5> 
< tr>
< td>< b>员工代码< / b>< / td>
<%= hidden_​​field'overtime_definition','employee_id',:value => params [:id]%>
< td><%= @ employee.employeeid%>< / td>
< td>< b>员工姓名< / b>< / td>
< td><%= @ employee.personnel.display_full_name%>< / td>
< / tr>
< tr>
< td>< b>部门< / b>< / td>
< td><%= @ employee.department? @ employee.department.name:%>< / td>
< td>< b>指定< / b>< / td>
< td><%= @ employee.designation? @ employee.designation.name:%>< / td>
< td>< b>位置< / 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>
半天超时工作时间
< / 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>
全日超时工作时间
< / 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 => 是薪水基础%>
< / tr>
< tr>
<%= od.sr_check_box:is_fixed_amount,{},true,false,:label => 是固定金额%>
< td colspan =2id =ov_hmstyle =display:none>
半天金额
<%= od.text_field:half_day_amount,:onkeypress => return numbersonly(event),:style => width:40px%>
< / td>
< td colspan =2id =ov_fmstyle =display:none>
全天金额
<%= od.text_field:full_day_amount,:onkeypress => return numbersonly(event),:style => width:40px%>
< / td>
< / tr>
<%end%>

< / table>



解决方案

您目前的编辑 link is:

 <%= link_toRe-Calculate,:action => edit,:id => employee.id,:flag =>重新计算%> 

在您的编辑操作中:


@overtime = OvertimeDefinition.find(params [:id] [:employee_id])##给我不能将Symbol转换为Integer错误。


根据编辑链接,您直接在查询参数中传递:id ,您可以访问 params [ :id] 。在你的params哈希中没有 params [:id] [:employee_id] ,所以当你说 params [:id] [:employee_id] code> Ruby尝试将:employee_id 转换为符号的整数。因此,错误。



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

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

@overtime =

OvertimeDefinition.find(params [:id])##给我找不到
查找OvertimeDefinition ID = 1353错误。实际上1353是雇员的
的id。


这是因为您在 params [:id]中传递 employee id code>所以显然这不会工作。您需要在这里传递OvertimeDefinition id。


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


您不传递任何:employee_id 编辑链接中的查询参数中。因此,params [:employee_id]将为nil,而 find 方法将失败,因为您未传递任何ID。



解决方案:



更新编辑连结如下:

 <%= link_toRe-Calculate,:action => edit,:id => @ overtimedefinition.id,:employee_id => employee.id,:flag =>重新计算%> 

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



更新更新为:

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


I have two models Employee and Overtime Definition The Associations are set like this

Employee

class Employee < ActiveRecord::Base

has_many :overtime_definitions

Overtime Definition

class OvertimeDefinition < ActiveRecord::Base

belongs_to :employee

I created an Overtime definition for an employee and it all looks fine.However I'm having trouble with editing the same for an 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

Tried with these in the edit method

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

    #gives me can't convert Symbol into Integer error.

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

    #gives me Couldn't find OvertimeDefinition with ID=1353 error.Actually 1353 is the id of that employee.

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

    #gives me couldn't find OvertimeDefinition without an ID error.

My _search_overtime_employee_list having these links for new and edit actions

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

Employee Details

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

I just lost out here completely in getting that edit action work.Any help is greatly appreciated!

解决方案

Your current edit link is:

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

In your edit action:

@overtime = OvertimeDefinition.find(params[:id][:employee_id]) ## gives me can't convert Symbol into Integer error.

As per the edit link, you are directly passing :id in query params which you can access as params[:id]. There is no params[:id][:employee_id] in your params hash so when you say params[:id][:employee_id] Ruby tries to convert :employee_id to an integer which is a symbol. Hence, the error.

I think you should be passing id of OvertimeDefinition record in :id from your link. And access it as

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

in the Controller's action.

@overtime = OvertimeDefinition.find(params[:id]) ## gives me Couldn't find OvertimeDefinition with ID=1353 error.Actually 1353 is the id of that employee.

This is because you are passing employee id in params[:id] so obviously this will not work. You need to pass OvertimeDefinition id here.

@overtime = OvertimeDefinition.find(params[:employee_id]) ## gives me couldn't find OvertimeDefinition without an ID error.

You are not passing any :employee_id in query params within edit link. So, params[:employee_id] will be nil and find method fails because you didn't pass any id to it.

Solution :

Update your edit link as below:

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

Replace @overtimedefinition.id with appropriate id of OvertimeDefinition record. As you have not shared the code, I don't know the name of OvertimeDefinition variable.

Update your edit action as:

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

这篇关于无法获得编辑功能工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆