【发布时间】:2020-07-03 08:48:12
【问题描述】:
下面是我的类别控制器:
class CategoryController < ApplicationController
def index
@category = Category.all
end
def new
@category = Category.new
end
def create
@category = Category.new(params[:category])
@category.save
redirect_to @category
end
def show
@category = Category.find(params[:id])
end
private
def category_params
params.require(:category).permit(:name)
end
def find_post
@category = Category.find(params[:id])
end
end
这是我想要显示所有类别的索引页面
<h1>ALL CATEGORIES</h1>
<% @category.each do |c|%>
<ul><%= link_to c.name, categories_path(category) %></ul>
<% end %>
<%= link_to "Add Category", new_category_path %>
这些是我为这个控制器设置的路由
Rails.application.routes.draw 做
root 'category#index'
post '/category' => 'category#create'
get '/category/new' => 'category#new', as:'new_category'
get '/category/:id' => 'category#show', as:'categories'
结束
为什么我会收到此名称错误,我该如何解决?
未定义的局部变量或方法
category' forenter code 这里#<#<Class:0x00007feddc03ad50>:0x00007fede47f8378>你的意思是?@category
【问题讨论】:
-
嗨@Pulkit Kumar,我相信您的变量是
c而不是category。试试这个代码:<ul><%= link_to c.name, categories_path(c) %></ul>。另外,我建议您阅读以下内容:guides.rubyonrails.org/action_controller_overview.html 以获得最佳实践。 -
感谢先生您的回答,只需通过 c 即可解决
标签: ruby-on-rails ruby ruby-on-rails-3