-1

I have a rails app with a Listing model which has_many Courses which has_many Course_photos.

The rails route for loading new course_photos is "listing_course_photos" or /listings/:listing_id/courses/:id/course_photos(.:format)

In trying to load this form to upload course_photos I get the following error...

Error

ArgumentError in Listings#courses
Showing /Users/Jack_R/code/rails/planet_study/app/views/courses/_photo_upload.html.erb where line #1 raised:

First argument in form cannot contain nil or be empty

The Routes are nested like this:

resources :listings, except: [:edit] do
    member do
      *****
    end
        resources :courses, except: [:edit] do
          #delete :destroy, on: :collection
          member do
            ******
          resources :course_photos, only: [:create, :destroy]
        end
    end
  end

I'm uploading the photos from a partial views/courses/_photo_upload.html.erb:

<%= form_for @course, url: listing_course_photos_path([@listing, @course]), method: 'post', html: {multipart: true} do |f| %>
  <div class="row">
    <div class="form-group">
      <span class="btn btn-default btn-file text-babu">
        Select Photos
        <%= file_field_tag "images[]", type: :file, multiple: true %>
      </span>
    </div>
  </div>

  <div class="text-center">
    <%= f.submit "Add Photos", class: "btn btn-form" %>
  </div>

<% end %>
<br/>

<div id="course_photos"><%= render 'course_photos/course_photos_list' %></div>

Which in turn renders this partial:

<% if @course_photos.count > 0 %>
<br/>

<div class="row">
  <% @course_photos.each do |photo| %>
  <div class="col-md-4">
    <div class="panel panel-default">
      <div class="panel-heading preview">
        <%= image_tag photo.image.url %>
      </div>
    </div>
    <% end %>
  </div>
</div>

<% end %>

Course_Photos Controller

class CoursePhotosController < ApplicationController

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

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

    @listing.course = listing.course

    if params[:images]
      params[:images].each do |img|
        @course.course_photos.create[image: img]
      end

      @photos = @course.course_photos
      redirect_back(fallback_location: request.referer, notice: "Saved...")
    end
  end

private

  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

end

Any suggestions would be greatly appreciated

Jack Riminton
  • 130
  • 1
  • 8
  • You should initialize `@course` variable with `@listing.courses.new` or something like this – Peter Balaban Nov 01 '17 at 11:29
  • thanks @PetrBalaban I tried with this: "listing_course_photos_path([@listing, @listing.courses.new])" if that's what you meant? I got the same error – Jack Riminton Nov 01 '17 at 11:34
  • you need to use `@listing.courses.new` as first argument in `form_for`. But I suggest you to extract this assignment to `Listings#courses` – Peter Balaban Nov 01 '17 at 11:43
  • Thanks for the responses, there are probably too many things I'm doing wrong so I've decided to try a completely different method at a later date. Will keep this question updated once I've worked it out – Jack Riminton Nov 01 '17 at 14:55

1 Answers1

0

You should initialize @course variable in the controller

@course = @listing.courses.build

But it seems like you are doing uploading of files wrong. Maybe you should create/ edit a course and upload files with nested form. Or consider a multiple files upload(harder way).

kolas
  • 754
  • 5
  • 16
  • or send images[] next to @course, permit in strong attributes "images" in controller, then smth like this after Course.create `params[:images].each do |image| Course_photos.create(file: image,attachment, id: @course.id) end` – sonic Nov 01 '17 at 12:39
  • Thanks for the responses, there are probably too many things I'm doing wrong so I've decided to try a completely different method at a later date. Will keep this question updated once I've worked it out – Jack Riminton Nov 01 '17 at 14:55