I've got a rails app that has categories that have many startups. each startup belongs to a category. when I hit startups new button I get this error
ActiveRecord::RecordNotFound in StartupsController#new
Couldn't find Category without an ID
Here's the Startups Controller
class StartupsController < ApplicationController
before_action :set_startup, only: [:show, :edit, :update, :destroy]
respond_to :html
def index
@startups = Startup.all
end
show
end
def new
@category = Category.find(params[:category_id])
@startup = @category.startups.new
respond_with(@startup)
flash[:notice] = "Startup added."
end
def edit
end
def create
@category = Category.find(params[:category_id])
@startup = Startup.new(startup_params)
@startup.saved
end
def update
respond_to do |format|
if @startup.update(startup_params)
format.html { redirect_to @startup, notice: 'Startup was successfully updated.' }
format.json { render :show, status: :ok, location: @startup }
else
format.html { render :edit }
format.json { render json: @startup.errors, status: :unprocessable_entity }
end
end
end
def destroy
@startup.destroy
respond_to do |format|
format.html { redirect_to startups_url, notice: 'Startup was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_startup
@category = Category.find(params[:category_id])
@startup = Startup.find(params[:id])
end
def startup_params
params.require(:startup).permit(:category_id,:name, :date, :link)
end
end
EDIT: Routes File
Routes.rb
Rails.application.routes.draw do
resources :startups
resources :categories
root :to => 'categories#index'
end
startups form.html.erb
<%= form_for(@startup) do |f| %>
<% if @startup.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@startup.errors.count, "error") %> prohibited this startup from being saved:</h2>
<ul>
<% @startup.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :date %><br>
<%= f.text_field :date %>
</div>
<div class="field">
<%= f.label :link %><br>
<%= f.text_field :link %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Startup.rb model
class Startup < ActiveRecord::Base
belongs_to :category
end
Category model category.rb class Category < ActiveRecord::Base has_many :startups end
What am I missing?
I don't think it's a schema issue because I have category_id in my startups..
EDIT: The idea is that users will be able to 1. create a new category and add startups to that category and/or add startups to an existing category. The homepage will be an index of categories, users can click to view the category and view all the startups in that category. I have a startups_controller and a categories_controller.
EDIT: so I've been able to create a couple of "startups" but they aren't getting a category id. What's the best way for me to be sure they are assigned a category_id on create.