简体   繁体   English

Rails 4未初始化的常量Admin :: Category

[英]Rails 4 uninitialized constant Admin::Category

I have generated Admin namespaced Controllers for all my default models as follows: 我为所有默认模型生成了Admin命名空间控制器,如下所示:

rails g scaffold_controller admin/categories name:string slug:string description:string icon_xlarge:string icon_large:string icon_medium:string icon_small:string status:integer

This generated the following files: 这会生成以下文件:

Harshas-MacBook-Pro:nomad harshamv$ rails g scaffold_controller admin/categories name:string slug:string description:string icon_xlarge:string icon_large:string icon_medium:string icon_small:string status:integer
Plural version of the model detected, using singularized version. Override with --force-plural.
      create  app/controllers/admin/categories_controller.rb
      invoke  erb
      create    app/views/admin/categories
      create    app/views/admin/categories/index.html.erb
      create    app/views/admin/categories/edit.html.erb
      create    app/views/admin/categories/show.html.erb
      create    app/views/admin/categories/new.html.erb
      create    app/views/admin/categories/_form.html.erb
      invoke  test_unit
      create    test/controllers/admin/categories_controller_test.rb

app/model/category.rb 应用程序/模型/ category.rb

class Category < ActiveRecord::Base

  extend FriendlyId

  friendly_id :name, use: :slugged

  has_and_belongs_to_many :venues

end

app/controller/admin/categories_controller.rb 应用程序/控制器/管理/ categories_controller.rb

class Admin::CategoriesController < ApplicationController
  before_action :set_admin_category, only: [:show, :edit, :update, :destroy]

  # GET /admin/categories
  def index
    @admin_categories = Admin::Category.all
  end

  # GET /admin/categories/1
  def show
  end

  # GET /admin/categories/new
  def new
    @admin_category = Admin::Category.new
  end

  # GET /admin/categories/1/edit
  def edit
  end

  # POST /admin/categories
  def create
    @admin_category = Admin::Category.new(admin_category_params)

    if @admin_category.save
      redirect_to @admin_category, notice: 'Category was successfully created.'
    else
      render :new
    end
  end

  # PATCH/PUT /admin/categories/1
  def update
    if @admin_category.update(admin_category_params)
      redirect_to @admin_category, notice: 'Category was successfully updated.'
    else
      render :edit
    end
  end

  # DELETE /admin/categories/1
  def destroy
    @admin_category.destroy
    redirect_to admin_categories_url, notice: 'Category was successfully destroyed.'
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_admin_category
      @admin_category = Admin::Category.find(params[:id])
    end

    # Only allow a trusted parameter "white list" through.
    def admin_category_params
      params.require(:admin_category).permit(:name, :slug, :description, :icon_xlarge, :icon_large, :icon_medium, :icon_small, :status)
    end
end

app/view/admin/categories/index.html.erb 应用程序/视图/管理/类别/ index.html.erb

<h1>Listing admin_categories</h1>

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Slug</th>
      <th>Description</th>
      <th>Icon xlarge</th>
      <th>Icon large</th>
      <th>Icon medium</th>
      <th>Icon small</th>
      <th>Status</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @admin_categories.each do |admin_category| %>
      <tr>
        <td><%= admin_category.name %></td>
        <td><%= admin_category.slug %></td>
        <td><%= admin_category.description %></td>
        <td><%= admin_category.icon_xlarge %></td>
        <td><%= admin_category.icon_large %></td>
        <td><%= admin_category.icon_medium %></td>
        <td><%= admin_category.icon_small %></td>
        <td><%= admin_category.status %></td>
        <td><%= link_to 'Show', admin_category %></td>
        <td><%= link_to 'Edit', edit_admin_category_path(admin_category) %></td>
        <td><%= link_to 'Destroy', admin_category, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Category', new_admin_category_path %>

My Attempts 我的尝试

I edited the Controller as below 我编辑了Controller如下

 # GET /admin/categories
  def index
    @admin_categories = Category.all
  end

  # GET /admin/categories/1
  def show
  end

  # GET /admin/categories/new
  def new
    @admin_category = Category.new
  end

  # GET /admin/categories/1/edit
  def edit
  end

  # POST /admin/categories
  def create
    @admin_category = Category.new(admin_category_params)

    if @admin_category.save
      redirect_to @admin_category, notice: 'Category was successfully created.'
    else
      render :new
    end
  end

When I go to localhost/admin/categories and click "NEW category", I get the following error now: 当我转到localhost/admin/categories并单击“NEW category”时,我现在收到以下错误:

My routes file: 我的路线档案:

Rails.application.routes.draw do

  # Admin Routing
  namespace :admin do
    resources :categories, :cities, :countries, :lists, :oauths, :regions, :tags, :users, :user_groups, :venues, :venue_photos, :venue_reviews
  end

end

要访问命名空间之外的模型,您需要调用::Category.new而不是Admin::Category.new

You have resources :categories defined under the namespace :admin in your routes.rb ,so this line in your views/admins/categories/_form.html.erb 您有resources :categories namespace :admin下定义的resources :categories namespace :admin您的routes.rb namespace :admin ,因此您的views/admins/categories/_form.html.erb这一行

<%= form_for(@admin_category) do |f| %>

should be 应该

<%= form_for([:admin, @admin_category]) do |f| %>

For more info,refer this API 有关详细信息,请参阅此API

Update 更新

The second error is because of this line 第二个错误是因为这一行

params.require(:admin_category).permit(:name, :slug, :description, :icon_xlarge, :icon_large, :icon_medium, :icon_small, :status)

It should be 它应该是

params.require(:category).permit(:name, :slug, :description, :icon_xlarge, :icon_large, :icon_medium, :icon_small, :status)

As your error indicates, this is an issue with how you are calling your form. 如您的错误所示,这是您调用表单的方式的问题。 Your form needs to reference the admin namespace, like this: 您的表单需要引用admin命名空间,如下所示:

<%= form_for [:admin, @category] do |f| %>

However, there are a number of things different about the way the scaffold built your docs from how I would recommend. 然而,脚手架根据我的推荐建立你的文档的方式有很多不同之处。

I would also suggest simplifying the code in the scaffolded controller to reference simple @category and @categories, rather than @admin_category and @admin_categories. 我还建议简化scaffolded控制器中的代码,以引用简单的@category和@categories,而不是@admin_category和@admin_categories。

Also, the model should not be in the admin namespace, so Admin::Category.new should be Category.new. 此外,模型不应位于admin命名空间中,因此Admin :: Category.new应为Category.new。 The rest of the model calls should be adjusted accordingly as well. 其余的模型调用也应相应调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM