Ruby on Rails使用现有的数据库ID无法正常工作

时间:2017-11-13 16:06:03

标签: mysql ruby-on-rails activerecord

我有一个使用现有数据库的Ruby on Rails应用程序。现有数据库的字段为id,这是数据库中的自动增量主键,但Ruby on Rails似乎没有使用它,因为我收到First argument in form cannot contain nil or be empty的错误

我的edit.html.erb是:

<%= form_for (@activevulnerabilities) do |f| %>
  <div class="row">
    <div class="col-xs-12">
      <div class="box">

        <div class="box-header">
          <h3 class="box-title">Edit Vulnerability Type</h3>
        </div>

        <div class="box-body">
          <%= render partial: 'admin/active_vulnerabilities/form', locals: { f: f } %>
        </div>

        <div class="box-footer">
          <%= link_to 'Cancel', active_vulnerabilities_path, class: 'btn btn-danger' %>
          <%= f.submit 'Submit', class: "btn btn-success" %>
        </div>

      </div>
    </div>
  </div>
<% end %>

我的控制器是:

class Admin::ActiveVulnerabilitiesController < ApplicationController
  layout :layout_by_resource
  def index
    @activevulnerabilities = ActiveVulnerability.all
  end

  def edit
    @activevulnerability = ActiveVulnerability.find(params[:id])
  end

  private def layout_by_resource
    if devise_controller?
      'layouts/devise'
    else
      'admin/layouts/admin'
    end
  end
  private def set_active_vulnerability
    @activevulnerability = ActiveVulnerability.find(params[:id])
  end
end

1 个答案:

答案 0 :(得分:1)

在您的修改操作中,您设置了@activevulnerability变量,并且您正在尝试访问@activevulnerabilities变量nil。修复变量命名,它应该没问题。