发布于 2025-01-01 02:54:13 字数 8 浏览 2 评论 0原文

continue

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岛徒 2025-01-08 02:54:13

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文