I'm making a web app to track ideas that are within categories. The categories are saving fine, but the ideas that belong to the categories are not. The ideas are also visible on show, but not index. I tried switching up the idea_params so they don't have the permit, because when it did it gave me a missing param error.
IdeasController
def index
@ideas = policy_scope(Idea)
# @ideas = Idea.all
end
def new
@idea = Idea.new(idea_params)
authorize @idea
end
def create
@idea = Idea.new(idea_params)
@idea.category = @category
@idea.user = current_user
authorize @idea
if @idea.save
redirect_to category_ideas_path(@category)
else
render :new
end
end
def show
authorize @idea
end
private
def find_category
@category = Category.find(params[:category_id])
end
# def find_user
# @user = current_user.id
# end
def idea_params
params.permit(:title, :description, :status, :category_id)
# params.require(:idea).permit(:title, :description, :status, :category_id)
end
Idea Index
<ul>
<% if @category.ideas.nil? %>
<p>Start adding ideas</p>
<% else %>
<% @category.ideas.each do |idea| %>
<li>
<h1> <%= idea.title %> </h1>
<p> <%= idea.description %> </p>
</li>
<% end %>
<% end %>
</ul>
Idea New
<div class="form-inputs">
<%= f.input :title %>
<%= f.input :description %>
<%= f.input :status, priority: ['incomplete'], collection: [ "incomplete", "in progress", "done"] %>
</div>
<div class="form-actions">
<%= f.button :submit, class:'btn btn-outline-primary' %>
</div>
<% end %>
Idea Model
class Idea < ApplicationRecord
belongs_to :category
belongs_to :user
validates :title, presence: { message: 'Cannot be blank' }
end
Idea Category Model
class IdeaCategory < ApplicationRecord
belongs_to :idea
belongs_to :category
has_many :ideas, through: :idea_categories
end
Category Model
class Category < ApplicationRecord
has_many :idea_categories, dependent: :destroy
has_many :ideas, through: :idea_categories, source: :idea
belongs_to :user
end