我一直在关注创建博客的Rails教程。除了文章,我决定创建和分类。但是,我一直在出错
undefined method `categories_path' for #<#<Class:0x3a26d50>:0x37f99f8>
当我一直使用它时,我不知道这是一个错误的变量“ @category”。
耙道:
这是其余的代码:
class CategoryController < ApplicationController
def index
@category = Category.all
end
def new
@category = Category.new
end
def edit
@category = Category.find(params[:id])
end
def show
@category = Category.find(params[:id])
end
def create
@category = Category.new(category_params)
if @category.save
redirect_to @category
else
render 'new'
end
end
def update
@category = Category.find(params[:id])
if @category.update(category_params)
redirect_to @category
else
render 'edit'
end
end
def destroy
@category = Category.find(params[:id])
@category.destroy
redirect_to @category
end
private
def category_params
params.require(:category).permit(:name, :description, :category_primary)
end
end
new.html.rb
<h1>New category</h1>
<%= render 'form' %>
<%= link_to 'Back', category_path %>
_form.html.rb
<%= form_for @category do |f| %>
<% if @category.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@category.errors.count, "error") %> prohibited
this category from being saved:</h2>
<ul>
<% @category.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :name %><br>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :description %><br>
<%= f.text_area :description %>
</p>
<p>
<%= f.label :category_primary %><br>
<%= f.check_box :category_primary %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
你已经宣布路线Category
作为一个单一的资源。它们应该是复数形式:
# config/routes.rb
resources :categories
然后,在您的视图中,将所有路径名从单数更改为复数。例如,正确的路径是categories_path
,不是category_path
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句