简体   繁体   English

路由错误,没有路由匹配[GET]“/ categorys / new”

[英]Routing Error , No route matches [GET] “/categorys/new”

The name of the table is "category", controller is "categories". 表的名称是“类别”,控制器是“类别”。 I have added resources :categories in routes file. 我添加了resources :categories路径文件中的resources :categories

Why is it showing "Routing Error/ No route matches [GET] /categorys/new" ? 为什么显示"Routing Error/ No route matches [GET] /categorys/new" The following is the code in controller: 以下是控制器中的代码:

class CategoriesController < ApplicationController
  def new
    @category = Category.new
  end

  def create
    @category = category.build(category_params)

    if @category.save
      redirect_to root_path
    else
      render 'new'
    end
  end

  private

  def category_params
    params.require(:category).permit(:name)
  end
end

The route you've defined is named categories , but the URL in the error is categorys -- that shouldn't have come from a URL helper, but maybe you built the URL manually? 您定义的路由是命名categories ,但错误中的URL是categorys - 不应该来自URL帮助程序,但您是否可以手动构建URL? Either way, it doesn't match. 无论哪种方式,它都不匹配。

It's because resources :categories would produce GET /categories/new endpoint (along with others), but not GET /categorys/new . 这是因为resources :categories会产生GET /categories/new端点(以及其他端点),但不会产生GET /categorys/new See Rails routing guide for more info. 有关详细信息,请参阅Rails路由指南

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

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