[英]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. 我正在尝试验证课程模型中的表单输入,但是第二次尝试对任何表单输入进行:presence为true时,我收到以下错误。 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. 我想对所有内容都包含true,因此我已将true添加到所有数据点。
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 _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 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
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. 好的,这与我的控制器中的create动作没有变量@categories有关。 Throws up the error then. 然后抛出错误。 Found answer on this thread. 在此线程上找到答案。
Rails undefined method `map' for nil:NilClass - form collection_select 为nil:NilClass提供Rails未定义方法`map'-表单collection_select
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.