Rails 4.1:作业中的 NameError#new

Rails 4.1: NameError in Jobs#new

想为我的工作板显示不同的工作类别,使用简单的形式gem我已将以下内容添加到我的工作表中。

_form.html.erb

<%= simple_form_for(@job, html: { class: 'form-horizontal' }) do |f| %>

<%= f.collection_select :category_id, Category.all, :id, :name, {prompt: "Choose a category" }, input_html: { class: "dropdown-toggle" } %>


    <%= f.input :title, label: "Job Title", input_html: { class: "form-control" } %>
    <%= f.input :description, label: "Job Description", input_html: { class: "form-control" } %>
    <%= f.input :company, label: "Your Company", input_html: {class: "form-control" } %>
    <%= f.input :url, label: "Link to Job", input_html: { class: "form-control" } %>
    <br/>
    <div class="form-group">
    <%= f.submit class: "btn btn-primary" %>
  </div>
<% end %>

但是当我转到 jobs.new.html 时它会生成以下错误

作业中的名称错误#new 显示第 3 行出现的 /Users/neilpatel/Desktop/Rails/jobs_board/app/views/jobs/_form.html.erb: 未初始化的常量 ActionView::CompiledTemplates::Category

<%= simple_form_for(@job, html: { class: 'form-horizontal' }) do |f| %>

  **<%= f.collection_select :category_id, Category.all, :id, :name, {prompt: "Choose a category" }, input_html: { class: "dropdown-toggle" } %>** -<error


    <%= f.input :title, label: "Job Title", input_html: { class: "form-control" } %>

错误表明您的应用程序中没有 Category 模型。这就是为什么 rails 将 Category 视为常量并抛出此错误 uninitialized constant。尝试在您的 app/models 目录中添加 Category 模型。

<%= f.collection_select :category_id, Category.all, :id, :name, {prompt: "Choose a category" }, input_html: { class: "dropdown-toggle" } %>

Category.all 应该是 Modelname.all