I have a model Resources that when a new entry is made I want to be able to assign a category to it. Code is setup as so:
Resource.rb
has_many :categories
category.rb
has_many_and_belongs_to :resources
(resources)_form.html.erb (along with the rest of resources fields)
<%= form_for @resource do |f| %>
<%= f.select :category, Category.all, :prompt => "Category" %>
<%= end %>
categories_controller.rb
class CategoriesController < ApplicationController
def new
@category = Category.new
end
def edit
@category = Category.find(params[:id])
end
def create
@category = Category.new(category_params)
@category.save
#redirect_to @category
end
def update
@category = Category.find(params[:id])
#if @category.update(params[:category].permit(:category_params))
# redirect_to @category
#else
# render 'edit'
#end
end
def destroy
@category = Category.find(params[:id])
@category.destroy
#redirect_to categorys_path
end
private
def category_params
params.require(:category).permit(:name)
end
end
There will only be 4 or so categories and they won't change. I only need to be able to assign a category via dropdown when creating a resources entry, and list entries under a certain category.
right now, I get the error "undefined method 'category'" when trying to view resources/new.
Any easy to understand (rails beginner) help is appreciated