更新操作似乎什么也不做 [英] Update action seems to be doing nothing
问题描述
目前,我有一个 overtime_definition_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] [:分] .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 = Employee.find(params [:employee_id])
end
def update
@employee = Employee.find(params [:id] )
@overtime = OvertimeDefinition.find(params [:id])
如果@ overtime.update_attributes(params [:overtime_definition])
flash [:notice] = {@ 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])
如果request.xml_http_request?
render:update do | page |
page ['search_result_div']。replace_html:partial => search_overtime_employee_list
end
end
end
$ b
更新前的值: id = 235
, employee_id = 1353
和其余字段。
更新后的值:
id = 235
, employee_id = 235
和其他值(与更新前相同的值)。
但是, employee_id
overtime_definition
的值
id
。很奇怪。
edit.rhtml
<%= form_tag:action => 'update',:id => @ overtime.id,:employee_id => @ employee.id,:flag => params [:flag]%>
<%= 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>
< / fieldset>
_search_overtime_employee_list.rhtml
< table cellspacing =0cellpadding =0width =100%id ='type'>
< tr class =tblebluehead>
< th>
SI.NO
< / th>
< th>
Code
< / th>
< th>
名称
< / th>
< th>
Department
< / th>
< th>
名称
< / th>
< th colspan =3>
函数
< / th>
< / tr>
<%count = 0%><%i = 0%>
<%for @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)
<%end%>
< / td>
< td>
<%= h employee.department? employee.department.name:%>
< / td>
< td>
<%= h employee.designation? employee.designation.name:%>
< / td>
< td>
<%= link_to计算,:action => new,:id => employee.id,:flag =>Calculation%>
< / td>
<%for @overtimes%>
<%if overtime.employee_id == employee.id%>
< td>
<%= link_to重新计算,:action => edit,:id => overtime.id,:employee_id => employee.id,:flag =>重新计算%>
< / td>
<%end%>
<%end%>
< / tr>
<%end%>
< / table>
< b> <%= will_paginate @active_profile,:next_label => & gt;& gt; ,:prev_label => & lt;& lt;%>< / b>
UPDATE
这些是我更新操作时lof文件的参数。
开始POST/ edms / overtime_definitions / update / 249 ?employee_id = 1353& flag = Re-Calculatefor 127.0.0.1在2014-04-09 15:12:45 +0530
由Edms处理:OvertimeDefinitionsController#update as html
参数:{ 1353,is_salary_basis=>1,is,=,utf8=>>,authenticity_token=>>,authenticity_token=>NT + iv9FBJy87aabbNLALf50LkKv07yOR7xFA9Oe3ijs =,overtime_definition=& is_fixed_amount=>0,half_day_amount=>,full_day_amount=>},half_day_extra_duty_hours=> {year=>2000,month=> ;1,day=>1,hour=>02,minute=>02},full_day_extra_duty_hours=> {year=& 2000,month=>1,day=>1,hour=>04,minute=>04},commit=> Update,employee_id=>1353,flag=>重新计算,id=>249}
UPDATE 1
已更改 _form.rhtml中的隐藏字段如下:
<%= hidden_field'overtime_definition','employee_id',:value => @employee .id%>
UPDATE 2
像这样更改@employee实例
@employee = Employee.find(params [:employee_id])
UPDATE 3
更改 edit.rhtml 中的form_tag参数,例如
<%= form_tag:action = ; 'update',:id => @ overtime.id,:employee_id => params [:employee_id],:flag => params [:flag]%>
现在它不会将
employee_id
更新后,id overtime_definition
,但是值未更新。
我怀疑
编辑表单
params
或编辑/更新
任何帮助都非常感谢!
解决方案
def update
@employee = Employee.find(params [:id])
@overtime = OvertimeDefinition.find(params [:id])
$ b您可能会更好:
def update
@employee = Employee.find(params [:id])
@overtime = OvertimeDefinition.find(params [:employee_id ])
如果发布了params哈希值,位置来帮助,但这是你的问题的核心,使用
Graeme McLean
的信息太Currently,I have a
overtime_definition_controller
with methods like thisclass 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
However when i tried to update,it does nothing.
The values before update:
id = 235
,employee_id = 1353
and remaining fields.The values after update:
id = 235
,employee_id = 235
and remaining values(same values as before update).However,It is converting
employee_id
value toid
of thatovertime_definition
. It is weird.edit.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" %>
_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> </fieldset>
_search_overtime_employee_list.rhtml
<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 => ">>" ,:prev_label => "<<"%></b>
UPDATE
These are the parameters of lof file when i do update action.
Started POST "/edms/overtime_definitions/update/249?employee_id=1353&flag=Re-Calculate" for 127.0.0.1 at 2014-04-09 15:12:45 +0530 Processing by Edms::OvertimeDefinitionsController#update as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"NT+iv9FBJy87aabbNLALf50LkKv07yOR7xFA9Oe3ijs=", "overtime_definition"=>{"employee_id"=>"1353", "is_salary_basis"=>"1", "is_fixed_amount"=>"0", "half_day_amount"=>"", "full_day_amount"=>""}, "half_day_extra_duty_hours"=>{"year"=>"2000", "month"=>"1", "day"=>"1", "hour"=>"02", "minute"=>"02"}, "full_day_extra_duty_hours"=>{"year"=>"2000", "month"=>"1", "day"=>"1", "hour"=>"04", "minute"=>"04"}, "commit"=>"Update", "employee_id"=>"1353", "flag"=>"Re-Calculate", "id"=>"249"}
UPDATE 1
Changed the hidden field in _form.rhtml like this
<%= hidden_field 'overtime_definition','employee_id',:value=>@employee.id %>
UPDATE 2
Changed the @employee instance like this
@employee = Employee.find(params[:employee_id])
UPDATE 3
Changed the form_tag params in edit.rhtml like this
<%= form_tag :action => 'update',:id => @overtime.id,:employee_id => params[:employee_id],:flag=> params[:flag] %>
Now it is not converting
employee_id
value toid
ofovertime_definition
after update action,but the values are not updating though.I suspect either of the
edit form
params
oredit/update
actions in the controller could be causing the problem.Can't figure out what it is.Any Help is greatly appreciated!
解决方案def update @employee = Employee.find(params[:id]) @overtime = OvertimeDefinition.find(params[:id])
So your two models are populated with exactly the same data?
You'll probably be better with:
def update @employee = Employee.find(params[:id]) @overtime = OvertimeDefinition.find(params[:employee_id])
If you post the params hash you get in your logs, I'll be in a better position to help, but this is the core of your problem, using
Graeme McLean
's info too这篇关于更新操作似乎什么也不做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!