Reputation: 457
I'm a new developer in ROR. I use devise authentication for sign_in or sign_up, it works very well, but when i select data(category or sub_category) from database in app/views/layouts/application.html.erb
i get some errors as shown bellow:
NoMethodError in Users::Sessions#new
Showing C:/railsapp/facepro/app/views/layouts/application.html.erb where line #28 raised:
undefined method `each' for nil:NilClass
Extracted source (around line #28):
<% @categories.each do |category| %>
<li><a href="#"><%= category.category_name %></a>
<ul class="dropdown-menu">
please help me!
Upvotes: 1
Views: 1469
Reputation: 76774
The accepted answer should not be accepted - it doesn't fix the core of the problem.
--
Devise
The core of your problem has nothing to do with Devise.
You clue is in the error itself:
Showing C:/railsapp/facepro/app/views/layouts/application.html.erb where line #28
Devise has absolutely no bearing on the application
layout
- it's only for user authentication. To fix this, you need to ensure you have the @categories
variable defined every time you load the application layout
--
ApplicationController
To do this, you'll need to declare that variable each time you load a controller action. As every controller should inherit from the application
controller, the way to do this is to use the before_action
callback in your app/controllers/application_controller.rb
file:
#app/controllers/application_controller.rb
Class ApplicationController < ActionController::Base
before_action :set_categories
private
def set_categories
@categories = Category.all
end
end
Upvotes: 1
Reputation: 6981
Going to guess that you haven't defined @categories
in Sessions#new
.
In SessionsController
you will need something like this:
def new
@categories = Category.all
...
end
Upvotes: 0