在學習Rails 4時,我試圖將類別添加到簡單博客的帖子時停滯不前。我生成了模型,運行遷移並添加了一個控制器。無論我現在在創建類別時做什麼,我都會遇到同樣的錯誤:沒有路由匹配[POST],這很奇怪,因爲我似乎擁有所有的代碼。請幫忙!Rails 4路由錯誤 - 沒有路由匹配[POST]「/ categories/new」
類別控制器
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
的routes.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
category.rb
class Category < ActiveRecord::Base
validates :name, presence: true
has_many :posts
end
new.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"
你可以添加整個回溯? –