Bradley
Bradley

Reputation: 932

Rails validations throwing an undefined method error map with simple_form

I'm trying to validate my form inputs in the course model however the second I try to validate :presence to true with any form input I receive the below error. Something to do with the map collection.

undefined method `map' for nil:NilClass

The error is throwing up this line of code in my form. It's to do with categories map. If I have no validations and create my course leaving everything blank it created the course. I want to include presence true for everything so I've added true to all the data points.

validates :course_reference, :title, :course_img_file_name, :category_id, :description, :short_description, :venue, :location_id, :duration_days, :duration_weeks, :start_date, :start_time, :end_date, :max_enrolment, :price, :presence => true

Error occurred. I've reduced it to trying each form data on its own however the second I have any validates in the model I get the map error.

Can anyone help please.

_form.html.erb

 <div class="row">
        <div class="site-forms">
                <div class="col-md-10">
                <%= simple_form_for @course do |f| %>
                    <%= f.input :course_reference, placeholder: "Course Reference", required: true, label: false %>
                        <!-- <= f.input :course_img, as: :file, required: true, label: "Please upload a brand image for your course" %><br> -->
                        <span class="btn btn-default btn-file">
                            <i class="fa fa-cloud-upload fa-lg"></i> Upload Image
                            <%= f.input :course_img, as: :file, required: true, label: false %>
                        </span>  Please keep images to 225hx300w for best display settings <br><br>
                    <%= f.input :title, placeholder: "Course Title", required: true, label: "Course Title" %>
                    <%= select_tag(:category_id, options_for_select(@categories), :prompt => "Select Category") %><br><br>
                    <%= f.input :description, as: :wysihtml5, placeholder: "**** NOTE DO NOT USE HEADER 1,2 or 3 TAGS. THIS WILL NOT HELP SEO FOR YOUR COURSE ***", required: true, label: "Description (Please be descriptive about the courses content)" %>
                    <%= f.input :short_description, placeholder: "Please input a short description for this course", required: true %>
                    <%= f.input :venue, placeholder: "Venue full address", required: true, label: false %>
                    <%= select_tag(:location_id, options_for_select(@locations), :prompt => "County") %><br><br>
                    <%= f.input :duration_days, placeholder: "Total amount of days the course is run. Enter 0.5 if course runs for a half day. " , required: true, label: false %>
                    <%= f.input :duration_weeks, placeholder: "How many weeks does the course run", required: true %>
                    <%= f.input :start_date, required: true %>
                    <%= f.input :start_time, required: true %>
                    <%= f.input :end_date, required: true %>
                    <%= f.input :max_enrolment, placeholder: "Course capacity", required: true %>
                    <%= f.input :price, placeholder: "EUR", required: true %>
                    <%= f.button :submit, class: "btn btn-primary" %>
                    <% end %>
                </div>
        </div>
  </div>

Course.rb

class Course < ActiveRecord::Base

    validates :course_reference, :presence => true

  searchkick 

  belongs_to :user
  belongs_to :category
    belongs_to :location
  has_many :subscriptions, dependent: :destroy
    has_many :comments, dependent: :destroy

    geocoded_by :venue
    after_validation :geocode, if: :venue_changed?

  has_attached_file :course_img, styles: { course_index: "300x300>", course_show: "400x600>", course_search: "100x100" }, default_url: "/images/:style/missing.png"
  validates_attachment_content_type :course_img, content_type: /\Aimage\/.*\z/

    def price_in_cents
        price*100
    end
end

Upvotes: 0

Views: 476

Answers (1)

Bradley
Bradley

Reputation: 932

Found the answer after a bit more searching. Sorry lads.

Ok so it's to do with the create action in my controller not having the variable @categories in it. Throws up the error then. Found answer on this thread.

Rails undefined method `map' for nil:NilClass - form collection_select

Upvotes: 1

Related Questions