0

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.

user3787971
  • 457
  • 4
  • 22

1 Answers1

0

In your controller's new method:

change:

@category = Category.find(params[:category_id])

to:

@category = Category.new

And you are getting this error because your params does not have category_id so the action can't find the category.

Update

So, your categories have many startups. And, each startup belongs to a category.

In your categories model add this:

accepts_nested_attributes_for :startup

This accepts_nested_attributes_for method allows you to modify instances of Startup using the same mass-assignment facility on Category that makes simple forms so trivial.

Now, in your view, add this:

<%= f.fields_for :startup do |ff| %>
  <div>
    <%= ff.label :name %>
    <%= ff.text_field :name %>
  </div>
  <div>
    <%= ff.label :link %>
    <%= ff.text_field :link %>
  </div>
  <div>
    <%= ff.label :date %>
    <%= ff.text_field :date %>
  </div>
<% end %>

Basically, this way you are bulding a nested form and submitting your category and startup information to the controller and there it will be able create the category and corresponding startups.

Also, you should have @categoery as the outer object in the form and then nest the @startup inside the @category as shown above and should map to the categories_controller's new action.

See this tutorial for more information on building complex nested forms.

K M Rakibul Islam
  • 33,760
  • 12
  • 89
  • 110
  • I tried your suggestion but I'm still getting the error. I've made some edits above that might help. What do you think? – user3787971 Aug 31 '15 at 19:54
  • My answer will do it in one way. See this post: http://stackoverflow.com/questions/2034700/form-for-with-nested-resources This will also do it for you. Its a very similar use case as yours. – K M Rakibul Islam Aug 31 '15 at 20:32
  • ok thanks I'm looking through this now I'll update you in a bit but this does make sense! appreciate it. I feel like this is a pretty common use case I've run into so I'm stoked to figure it out. – user3787971 Aug 31 '15 at 20:39