Sam Baumgarten
Sam Baumgarten

Reputation: 2249

Im getting this error in my rails app: undefined method `course_users_path' for #<#<Class:0x10fd9be48>:0x10fd93d60>

I have a rails app with a has_and_belongs to many association, I keep getting this error whenever I goto the URL for this controller:

Showing /Users/Sam/makrrEdu/app/views/enroll/_form.html.erb where line #1 raised:

undefined method `course_users_path' for #<#<Class:0x10fd9be48>:0x10fd93d60>

Extracted source (around line #1):

1: <%= form_for(@userC) do |f| %>
2:   <% if @userC.errors.any? %>
3:     <div id="error_explanation">
4:       <h2><%= pluralize(@userC.errors.count, "error") %> prohibited this enroll from being saved:</h2>

Trace of template inclusion: app/views/enroll/new.html.erb

Rails.root: /Users/Sam/makrrEdu

Application Trace | Framework Trace | Full Trace
 app/views/enroll/_form.html.erb:1:in `_app_views_enroll__form_html_erb___794935172_2280224940'
app/views/enroll/new.html.erb:3:in `_app_views_enroll_new_html_erb___1934508758_2280307340'
app/controllers/enroll_controller.rb:24:in `new'

Heres my code:


enroll_controller.rb

class EnrollController < ApplicationController
  def new
    @userC = CourseUser.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render :json => @userC }
    end
  end
  def index

  end
  def create
    @userC = CourseUser.new(params[:course])

    respond_to do |format|
      if @userC.save
        format.html { redirect_to @userC, :notice => 'Enroll was successfully created.' }
        format.json { render :json => @userC, :status => :created, :location => @userC }
      else
        format.html { render :action => "new" }
        format.json { render :json => @userC.errors, :status => :unprocessable_entity }
      end
    end
  end
end

new.html.erb

<h1>New Enroll</h1>
<%= render 'form' %>
<%= link_to 'Back', courses_path %>

_form.html.erb

<%= form_for(@userC) do |f| %>
  <% if @userC.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@userC.errors.count, "error") %> prohibited this enroll from being saved:</h2>

      <ul>
      <% @userC.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
   <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :description %><br />
    <%= f.text_field :description %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

course_user.rb (model)

class CourseUser < ActiveRecord::Base
belongs_to :user
belongs_to :course
attr_accessible :course_id, :user_id
end

Upvotes: 1

Views: 183

Answers (1)

Sam Baumgarten
Sam Baumgarten

Reputation: 2249

I forgot to put resources :course_users in my routes.rb file.

Upvotes: 0

Related Questions