0

I've got two classes; a Day class and a CollegeClass class. The day is made up of many college classes and the day is created before a college class. The error im getting

ActionView::Template::Error (undefined method `day_college_classes_path' for#<#<Class:0xa0627dc>:0xa06bd3c>):
26: <% end %>
27: <% end %>
28: 
29: <%= form_for [@day, CollegeClass.new] do |f| %>
30: <%= f.text_field :module, placeholder: "Module" %>
31: <br>
32: <%= f.text_field :lecturer, placeholder: "Lecturer" %>
app/views/days/show.html.erb:29:in `_app_views_days_show_html_erb___616937818__626026478'

Here is my Day model

class Day
  include Mongoid::Document
  field :user, type: String
  field :date, type: String

  embeds_many :college_class
  accepts_nested_attributes_for :college_class
end

My College Class model

class CollegeClass
  include Mongoid::Document

  field :module,     type: String
  field :room,       type: String
  field :lecturer,   type: String
  field :start_time, type: String
  field :end_time,   type: String

  embedded_in :day

  embeds_many :notes
  embeds_many :tasks

  accepts_nested_attributes_for :notes
  accepts_nested_attributes_for :tasks
end

A college class is created on the show page for a Day. Here is the form for the creation of a college class:

<%= form_for [@day, CollegeClass.new] do |f| %>
<%= f.text_field :module, placeholder: "Module" %>
<br>
<%= f.text_field :lecturer, placeholder: "Lecturer" %>
<br>
<%= f.text_field :room, placeholder: "Room" %>
<br>
<%= f.text_field :start_time, placeholder: "Start Time" %>
<br>
<%= f.text_field :end_time, placeholder: "End Time" %>
<br>
<%= f.submit %>
<% end %> 

There is currently nothing in the show method in the day controller. These are the controller actions for the creation of a new College Class

  def create
    @day = Day.find(params[:id])
    @class = @day.college_class.create(class_params)
    redirect_to @day
  end

  private 

    def class_params
      params.require(:college_class).permit(:module, :lecturer, :room, :start_time, :end_time)
    end

The routes for the two models:

resources :days do 
  resources :college_class
end

Which gives us these:

 day_college_class_index GET    /days/:day_id/college_class(.:format)          college_class#index
                         POST   /days/:day_id/college_class(.:format)          college_class#create
   new_day_college_class GET    /days/:day_id/college_class/new(.:format)      college_class#new
  edit_day_college_class GET    /days/:day_id/college_class/:id/edit(.:format) college_class#edit
       day_college_class GET    /days/:day_id/college_class/:id(.:format)      college_class#show
                         PATCH  /days/:day_id/college_class/:id(.:format)      college_class#update
                         PUT    /days/:day_id/college_class/:id(.:format)      college_class#update
                         DELETE /days/:day_id/college_class/:id(.:format)      college_class#destroy
                    days GET    /days(.:format)                                days#index
                         POST   /days(.:format)                                days#create
                 new_day GET    /days/new(.:format)                            days#new
                edit_day GET    /days/:id/edit(.:format)                       days#edit
                     day GET    /days/:id(.:format)                            days#show
                         PATCH  /days/:id(.:format)                            days#update
                         PUT    /days/:id(.:format)                            days#update
                         DELETE /days/:id(.:format)                            days#destroy

I genuinely appreciate any & all help I get with this. Thanks.

8
  • as per your route table, I can see the path as day_college_class, thus it should be day_college_class_path. Commented Jun 6, 2014 at 19:07
  • 1
    @ArupRakshit That isn't the right answer. The solution is to fix the resources, as below. Resources should always be plural, not singular. Commented Jun 6, 2014 at 19:09
  • @meagar Humm.. That I can see, someone answered. How would I guess, what actual name OP want. Both way it is true. If OP wrote route properly, then may be he typed the path as a typo,,, or what you said. Commented Jun 6, 2014 at 19:11
  • @ArupRakshit it was a typo mistake. I've been looking at it for about an hour and for some reason I completely forgot to pluralise the college_class routes. Even though the days one was pluralised and staring right at me. Commented Jun 6, 2014 at 19:16
  • @ArupRakshit Because of Rails? Familiarity with Rails. There is an objectively correct answer to this question, and it isn't the one you suggested. He doesn't even have the option to use day_college_class_path as you suggest. Rails is generating that name based on the name of his model. Commented Jun 6, 2014 at 19:17

1 Answer 1

2

Try changing your routes.rb to this

resources :days do 
  resources :college_classes
end
Sign up to request clarification or add additional context in comments.

1 Comment

Just did it there. Had to wait 10 minutes for it. Again, thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.