表单属性不是nil,但获取First表单属性不能是nil错误

时间:2016-01-12 09:57:36

标签: ruby-on-rails forms

我的编辑用户模式出现错误,说第一个参数不能为零;但是,表单可以正常工作并提取信息。设置如下: 1)我有一张表总结了员工 2)每个员工都有一个修改动作 3)修改操作使用编辑表单

打开一个模态

表单有效,用户可以编辑员工。但是,每次打开时,我都会收到一条内部错误通知,说明第一个参数不能为空或为空。为了确保这一点,我在编辑模式中显示员工姓名,以确认它不是零(并且不是)。问题是,我收到了由此问题引起的内部服务器错误的通知,尽管表单工作正常。

有关可能导致此问题以及如何解决问题的任何想法?

员工表代码:

    <tr>
    <td> <%= employee.name %> </td>
    <td> <%= employee.status ? t(:active) : t(:inactive) %> </td>
    <td> <%= mmm_dd_yy_date(employee.created_at) %> </td>
    <td> <%= mmm_dd_yy_date(employee.updated_at) %> </td>
    <td> <%= link_to t(:edit), '#', "data-toggle" => "modal", "data-target" => "#EditModal_#{employee.id}", "data-remote" => edit_employee_path(employee) + "#modal-edit-form" %> | 
  <%= link_to t(:remove), employee, method: :delete, data: { confirm: t(:confirm_remove_employee, name: employee.name)} %>
     <!-- Modal -->
      <div id='<%= "EditModal_#{employee.id}" %>' class="modal fade" role="dialog">
       <div class="modal-dialog">
         <!-- Modal content-->
         <div class="modal-content">
           <div class="modal-header">
             <button type="button" class="close" data-dismiss="modal">&times;</button>
             <h4 class="modal-title"><%= t(:edit_employee) %></h4>
           </div>
           <div class="modal-body"> 
             <%=render partial: 'edit_form', locals: {employee: employee} %>
           </div>
           <div class="modal-footer">
             <button type="button" class="btn btn-default" data-dismiss="modal"><%= t(:close) %></button>
           </div>
         </div>
       </div>
     </div>
   </td>
</tr>

表格模式代码:

<div id= "modal-edit-form">
  <%= employee.name %>
<%=form_for(employee) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class = "row" >
    <div class = "col-xs-6">
      <%= f.label :name%>
      <%= f.text_field :name, class: 'form-control' %>
      <%= f.label :status %>
      <%= f.collection_select :status, {true: t(:yes), false: t(:no)}, :first, :last, selected: employee.status %>
      <%= f.submit t(:update), class: "btn btn-primary"%>
    </div>
  </div>
<% end %>
</div>

1 个答案:

答案 0 :(得分:0)

代码对我来说很好。

-

我要说的一件事是,在同一页面上多次包含form_for是一个很大的禁忌 - 一堆冲突会发生(id等)。

我个人会将功能转移到您的employees#edit动作ajax:

#app/controllers/employees_controller.rb
class EmployeesController < ApplicationController
   respond_to :js, :html, :json, only: :edit
   def edit
      @employee = Employee.find params[:id]
      respond_with @employee
   end
end 

#app/views/employees/edit.js.erb
$("<%=j render 'edit' %>").appendTo("body");
// fire modal

#app/views/employees/edit.html.erb
<div id='<%= "EditModal_#{employee.id}" %>' class="modal fade" role="dialog">
   <div class="modal-dialog">
     <!-- Modal content-->
     <div class="modal-content">
       <div class="modal-header">
         <button type="button" class="close" data-dismiss="modal">&times;</button>
         <h4 class="modal-title"><%= t(:edit_employee) %></h4>
       </div>
       <div class="modal-body"> 
         <%=render partial: 'edit_form', locals: {employee: @employee} %>
       </div>
       <div class="modal-footer">
         <button type="button" class="btn btn-default" data-dismiss="modal"><%= t(:close) %></button>
       </div>
     </div>
   </div>
</div>

#app/views/employees/index.html.erb
<%= link_to t(:edit), '#', data: { toggle: "modal", target: "#EditModal_#{employee.id}", remote: edit_employee_path(employee) + "#modal-edit-form" }, remote: true %>

这样,您只需在渲染时附加edit表单。