我为所有默认模型生成了管理命名空间控制器,如下所示:
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
这生成了以下文件:
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
应用程序/模型/类别.rb
class Category < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: :slugged
has_and_belongs_to_many :venues
end
应用程序/控制器/管理员/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
<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 %>
我的尝试
我编辑了控制器如下
# 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
当我转到localhost/admin/categories
并单击“新类别”时,我现在收到以下错误:
我的路线文件:
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