0

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
4
  • rake routes then look at the exact path name Commented Nov 1, 2017 at 15:11
  • This is the exact path name: "POST /listings/:listing_id/courses/:id/course_item(.:format) course_item#create" Problem is I don't understand why this isn't working Commented Nov 1, 2017 at 15:12
  • Please add the code for your models. Commented Nov 1, 2017 at 16:07
  • @Ankit Added above, thanks Commented Nov 1, 2017 at 16:11

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

And even <%= form_for ([@listing, @course, @course_item]) will be better
Thanks. I tried the first suggestion and got the same error as before. With the second one I got "First argument in form cannot contain nil or be empty "
@Jack Riminton you should also in Course_item#new change @listings = Listing.find(params[:listing_id]) to @listing = Listing.find(params[:listing_id]) :)
I also had an error in the routes. New error, using all of the suggestions above, is: "undefined method `listing_course_course_items_path' for #<#"
and if I change the form to "([@listing, @course, @course_item])" then the error is again: "First argument in form cannot contain nil or be empty"

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.