I'm probably trying too hard for my first website but i wanted to make a dropdown on a (bootstrap) navbar to be flexible, and show the names of the saved work categories.
This is what i've tried to do in the application.html.erb file :
<ul class="dropdown-menu">
<% @workcategory.each do |workcategory| %>
<li><%= workcategory.name%></li>
<% end %>
Failed with error undefined methodeach' for nil:NilClasson the<% @workcategory.each do |workcategory| %>` line.
This is the workcategories controller :
class WorkcategoriesController < ApplicationController
before_action :find_workcategory, only: [:edit, :update, :destroy]
def index
@workcategories = Workcategory.all.order("created_at DESC")
end
def new
@workcategory = Workcategory.new
end
def create
@workcategory = Workcategory.new(post_params)
if @workcategory.save
flash[:notice] = "Workcategory created"
redirect_to(:action=>'index', :workcategory_id => @workcategory.id)
else
@workcategories = Workcategories.order()
render('new')
end
end
def edit
end
def update
end
def destroy
@workcategory.destroy
redirect_to workcategory_path
end
private
def find_workcategory
@workcategory=Workcategory.find(params[:id])
end
def post_params
params.require(:workcategory).permit(:name)
end
end
Any tips and help are welcome, even non-related to the initial question :) Thank you
