ruby on rails - presence validation causes "undefined method `map' for nil:NilClass" error -



ruby on rails - presence validation causes "undefined method `map' for nil:NilClass" error -

i want people define course of study name before writing spotlight. did adding next code spotlight model

class spotlight < activerecord::base validates :name, presence: true end

before adding validation write spotlights without name. if seek next error message:

undefined method `map' 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 going on here? collection select base of operations ajax phone call fill other fields.

view

<%= form_for(@spotlight) |f| %> <% if @spotlight.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@spotlight.errors.count, "error") %> prohibited spotlight beingness saved:</h2> <ul> <% @spotlight.errors.full_messages.each |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 |format| if @spotlight.save format.html { redirect_to @spotlight, notice: 'spotlight 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 |format| if @spotlight.update(spotlight_params) format.html { redirect_to @spotlight, notice: 'spotlight 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 , songs based on genre selected colli = colli.where(name: params[:name]) # map name , id utilize in our options_for_select @teaser = colli.first.teaser end def destroy @spotlight.destroy respond_to |format| format.html { redirect_to spotlights_url } format.json { head :no_content } end end private # utilize callbacks share mutual setup or constraints between actions. def set_spotlight @spotlight = spotlight.find(params[:id]) end # never trust parameters scary internet, allow 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 explain seems problem? "map" function error referring to?

it looks @colli object nil.

<%= f.collection_select(:name, @colli, :name, :name, {prompt: 'selecteer een opleiding'}, {id: 'collis_select'}) %>

this has nil presence validation. create sure collection_select method receiving @colli instance variable. right it's receiving nil.

the map function instance method in class array. receives block, iterates on array, , returns new array elements returned block. can't phone call map on nil; error seeing above.

you can check if @colli nil raising it:

def load_colli @colli = colli.select(:name).distinct.order('name') raise @colli.to_s end

ruby-on-rails ruby validation ruby-on-rails-4

Comments

Popular posts from this blog

java Multi query from Mysql using netbeans -

c# - DotNetZip fails with "stream does not support seek operations" -

c++ - StartServiceCtrlDispatcher don't can access 1063 error -