私はカテゴリーコントローラを持っていると私は出力にホームページ内のすべてのカテゴリをしたいが、_menu.html.erbレイアウトで、私はこのエラーメッセージがあります。Railsのはnilオブジェクトエラー
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
は、私が管理者としてログインしてIすべてのカテゴリを追加、編集、削除、表示できます。
これは私のコードの一部です:
_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
答えを更新しました。 – iltempo
ありがとうございました:) – user1107922