Jack Riminton
Jack Riminton

Reputation: 130

Rails - undefined method error

I have the following models:

Listing has_many Courses which has_many Course_items

In a partial I am trying to create a form to create a Course_item.

In rake routes the route I am trying to use is listed as "/listings/:listing_id/courses/:id/course_item(.:format)"

However in trying to POST to this route I am getting the following error. Any help would be greatly appreciated.

Error

NoMethodError in Courses#show
Showing /Users/Jack_R/code/rails/planet_study/app/views/course_item/_new.html.erb where line #3 raised:

undefined method `listing_course_items_path' for #<#<Class:0x007ff8616081f8>:0x007ff866d3ba88>

The form is in the following partial:

<%= form_for ([@listing, @course.course_items.new]) do |f| %>
<ul>
<li><%= f.text_field :name %></li>
<li><%= f.date_field :start_date %></li>
<li><%= f.date_field :finish_date %></li>
</ul>
<% end %>

Course_item_controller.rb

class CourseItemController < ApplicationController

  before_action :set_listing
  before_action :set_course
  before_action :set_course_item, except: [:index, :new, :create]

  def index
    @listings = Listing.find(params[:listing_id])
    @courses = current_user.listings.courses
    @course_item = CourseItem.find(params[:id])
  end

  def new
    @listing = Listing.find(params[:listing_id])
    @courses = current_user.courses
    @course_item = current_user.listings.course.course_item.build
  end

  def create
    listing = Listing.find(params[:listing_id])
    course = Course.find(params[:course_id])

    @course_item = current_user.listing.course.course_items.new(course_item_params)
    if @course_item.save!
      flash[:notice] = "Saved!"
        redirect_back(fallback_location: request.referer)
    else
        flash[:alert] = "Something went wrong"
        render :new
    end
  end


end

def update
    if @course_items.update(course_item_params)
      flash[:notice] = "Saved..."
    else
      flash[:notice] = "Something went wrong..."
    end
    redirect_back(fallback_location: request.referer)
  end


private

  def course_items_params
    params.require(:course_item).permit(:name, :start_date, :finish_date, :price, :active)
  end

 def set_course
    @listing = Listing.find(params[:listing_id])
    @course = Course.find(params[:id])
  end

  def set_listing
    @listing = Listing.find(params[:listing_id])
  end

  def set_course_item
    @course_item = CourseItem.find(params[:id])
  end

Routes

resources :listings, except: [:edit] do
    member do
     ...
    end
        resources :listing_photos, only: [:create, :destroy]
        resources :courses, except: [:edit] do
          #delete :destroy, on: :collection
          member do
            ...
            resources :course_item do
              resources :reservations, only: [:create, :new, :index]
            end
        end
    end

Models

class Listing < ApplicationRecord
  belongs_to :user
  has_many :courses
  has_many :listing_photos
end

class Course < ApplicationRecord
  belongs_to :listing, optional: true
  has_many :course_items
end

class CourseItem < ApplicationRecord
  belongs_to :course
  has_many :reservations
end

Upvotes: 0

Views: 790

Answers (1)

Peter Balaban
Peter Balaban

Reputation: 643

You should use <%= form_for ([@listing, @course, @course.course_items.new]) in the view

Upvotes: 1

Related Questions