0
我有分類控制器和佈局_menu.html.erb我要輸出首頁所有類別,但我有此錯誤消息:軌,零對象錯誤
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
當我登錄的管理員我可以添加,編輯,刪除和查看所有類別。
這是我的代碼部分:
_menu.html.erb
<div class="menu">
<% @categories.each do |category| %>
<li>
<%= link_to category.title, category %>
</li>
<% end %>
</div>
Categories_controller.rb
def index
@title = "All categories"
@categories = Category.paginate(:page => params[:page])
end
def show
@category = Category.find(params[:id])
@title = @category.title
end
def new
@category = Category.new
@title = "Add category"
end
def create
@category = Category.new(params[:category])
if @category.save
flash[:success] = "Successfully added category"
redirect_to categories_path
else
@title = "Sign up"
render 'new'
end
end
def edit
@category = Category.find(params[:id])
@title = "Edit category"
end
def update
@category = Category.find(params[:id])
if @category.update_attributes(params[:category])
flash[:success] = "Category updated."
redirect_to categories_path
else
@title = "Edit user"
render 'edit'
end
end
def destroy
Category.find(params[:id]).destroy
flash[:success] = "User destroyed."
redirect_to categories_path
end
端
好的,但我應該如何解決這個問題?我應該在哪裏打電話@categories = Category.all?如果我將它添加到一個靜態頁面的家庭它的作品,但我不想將此添加到我的代碼中的每個方法。 – user1107922 2012-03-01 18:09:28
更新了答案。 – iltempo 2012-03-01 18:20:09
非常感謝你:) – user1107922 2012-03-01 18:23:49