I want people to define a course name before writing a spotlight. I did this by adding following code to the spotlight model
class Spotlight < ActiveRecord::Base
validates :name, presence: true
end
Before adding the validation I could write spotlights without any name. If I try that now I get following error message:
undefined method `map' for nil:NilClass
Extracted source (around line #29):
</div>
<div class="field">
<%= f.label :name, "Opleiding" %><br>
<%= f.collection_select(:name, @colli, :name, :name, {prompt: 'Selecteer een opleiding'}, {id: 'collis_select'}) %>
</div>
<div class="field">
<%= f.label :teaser %><br>
What is going on here? The collection select is the base for an ajax call I do to fill up other fields.
View
<%= form_for(@spotlight) do |f| %>
<% if @spotlight.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@spotlight.errors.count, "error") %> prohibited this spotlight from being saved:</h2>
<ul>
<% @spotlight.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :spotlight, "In de kijker" %><br>
<%= f.check_box :spotlight %>
</div>
<div class="field">
<%= f.label :start, "Start in de kijker" %><br>
<%= f.datetime_select :start %>
</div>
<div class="field">
ruby-on-rails
<%= f.label :stop, "Stop in de kijker" %><br>
<%= f.datetime_select :stop %>
</div>
<div class="field">
<%= f.label :name, "Opleiding" %><br>
<%= f.collection_select(:name, @colli, :name, :name, {prompt: 'Selecteer een opleiding'}, {id: 'collis_select'}) %>
</div>
<div class="field">
<%= f.label :teaser %><br>
<%= f.text_area :teaser, size: "85x10", id: 'teasers_select' %>
</div>
<div class="field">
<%= f.label :coursedate, "Startdatum opleiding" %><br>
<%= f.datetime_select :coursedate, id: 'startdate_select' %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<script>
$(document).ready(function() {
$('#collis_select').change(function() {
$.ajax({
url: "<%= update_teasers_path %>",
data: {
name : $('#collis_select').val()
},
dataType: "script"
});
});
});
</script>
Update teaser view
$('#teasers_select').val("<%= escape_javascript(@teaser) %>");
Controller
class SpotlightsController < ApplicationController
before_action :set_spotlight, only: [:show, :edit, :update, :destroy]
before_action :load_colli, only: [:new, :edit]
def index
@spotlights = Spotlight.all.order('spotlight DESC, start, stop')
end
def show
end
def new
@spotlight = Spotlight.new
end
def edit
end
def create
@spotlight = Spotlight.new(spotlight_params)
respond_to do |format|
if @spotlight.save
format.html { redirect_to @spotlight, notice: 'Spotlight was successfully created.' }
format.json { render action: 'show', status: :created, location: @spotlight }
else
format.html { render action: 'new' }
format.json { render json: @spotlight.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @spotlight.update(spotlight_params)
format.html { redirect_to @spotlight, notice: 'Spotlight was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @spotlight.errors, status: :unprocessable_entity }
end
end
end
def update_teasers
# updates artists and songs based on genre selected
colli = Colli.where(name: params[:name])
# map to name and id for use in our options_for_select
@teaser = colli.first.teaser
end
def destroy
@spotlight.destroy
respond_to do |format|
format.html { redirect_to spotlights_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_spotlight
@spotlight = Spotlight.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def spotlight_params
params.require(:spotlight).permit(:spotlight, :start, :stop, :name, :teaser, :coursedate)
end
def load_colli
@colli = Colli.select(:name).distinct.order('name')
end
end
Can somebody explain what seems to be the problem? What is the "map" function the error is referring to?