在学习 Rails 4 时,我在尝试为我的简单博客的帖子添加类别时遇到了困难。我已经生成了模型,运行了迁移并添加了一个控制器。无论我现在在尝试创建类别时做什么,我都会遇到同样的错误:没有路由匹配 [POST],这很奇怪,因为我似乎已经准备好了所有代码。请帮忙!
类别控制器
class CategoriesController < ApplicationController
def index
@categories = Category.all
end
def new
@category = Category.new
end
def create
@category = Category.new(category_params)
@category.save
redirect_to new_category_path, alert: "Category created!"
end
def show
@category = Category.find(params[:id])
end
def destroy
@category = Category.find(params[:id])
@category.destroy
redirect_to categories_path
end
private
def category_params
params.require(:category).permit(:name)
end
end
路线.rb
Blog::Application.routes.draw do
get 'tags/:tag', to: 'posts#index', as: :tag
resources :categories
resources :posts do
resources :comments
end
root 'welcome#index'
end
类别.rb
class Category < ActiveRecord::Base
validates :name, presence: true
has_many :posts
end
新的.html.erb
<%= form_for :category do |f| %>
<p>
<%= f.label :name %><br>
<%= f.text_field :name %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
/类别/新
No route matches [POST] "/categories/new"