Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8051553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:19:40+00:00 2026-06-05T07:19:40+00:00

I met very strange problem. I have Timetable model and try to write my

  • 0

I met very strange problem. I have Timetable model and try to write my custom validation. So, now i’m just trying to add test error for field to make sure that everything is ok. But it doesn’t work. So, i try to update object of Timetable model but when i don’t use my test custom validation everything works perfect. Otherwise i get such error:

NoMethodError in Timetables#update 

undefined method `map' for nil:NilClass

32: 
33:       <div class="controls">
34:         <%= f.select( :curriculum_id,                                                           
35:                       options_for_select( @subjects_with_curriculums,
36:                                           @tt.curriculum_id ),
37:                       { :include_blank => true }) %>
38:       </div>

Here is my model:

# == Schema Information
#
# Table name: timetables
#
#  id                  :integer         not null, primary key
#  curriculum_id       :integer
#  school_class_id     :integer
#  tt_day_of_week      :string(255)
#  tt_number_of_lesson :integer
#  tt_room             :string(255)
#  tt_type             :string(255)
#  created_at          :datetime        not null
#  updated_at          :datetime        not null
#

class Timetable < ActiveRecord::Base
  belongs_to :curriculum
  belongs_to :school_class

  has_many :lessons

  validates :school_class_id, :presence => { :message => "should exist" }

  validates :tt_day_of_week,
              :presence  => true,
              :inclusion => { :in => %w(Mon Tue Wed Thu Fri) }

  validates :tt_number_of_lesson,
              :presence => true,
              :inclusion => {
                              :in => 1..9,
                              :message => "should have 1..9 symbols"
                            }

  validates :tt_room,
              :length => {
                           :maximum => 3,
                           :message => "should have 3 symbols"
                         },
              :allow_blank => true

  validates :tt_type,
              :inclusion => { :in => ["Primary lesson", "Extra"] },
              :allow_blank => true

  validate :test

  def test
    errors.add(:tt_number_of_lesson, "test")
  end
end 

My controller:

# encoding: UTF-8
class TimetablesController < ApplicationController
  ...

  def edit
    @types_of_lesson = collect_types_of_lesson
    @tt = Timetable.find( params[:id] )
    @subjects_with_curriculums = collect_subjects_with_curriculums( @tt.school_class )
  end

  def update
    @tt = Timetable.find( params[:id] )

    if @tt.update_attributes( params[:timetable] )
      flash[:success] = "Расписание успешно обновлено!"
      redirect_to timetables_path
    else
      flash.now[:error] = @tt.errors.full_messages.to_sentence :last_word_connector => ", ",
                                                               :two_words_connector => ", "
      render 'edit'
    end
  end

  private
    # Collecting subjects names for school class and curriculum_id for each subject.
    def collect_subjects_with_curriculums( school_class )
      subjects = school_class.curriculums.collect do |c|
        [ c.qualification.subject.subject_name, c.id  ]
      end
    end

    def timetable_for_class_with_existance_data( school_class )
      return [] if Timetable.all.empty?

      Timetable.select do |t|
        ( t.school_class.class_code == school_class.class_code ) and
        not ( t.tt_room.blank? ) and not ( t.tt_type.blank? ) and
        not ( t.curriculum_id.nil? )
      end.to_a
    end

    # Return for school class it's timetable.
    def timetable_for_class( school_class )
      Timetable.select{|t| t.school_class.class_code == school_class.class_code }.to_a
    end

    def subjects_of_class( school_class )
      subjects = school_class.curriculums.collect do |c|
        c.qualification.subject.subject_name
      end
    end

    # Return sorted by number of lesson tometable for one day.
    def sorted_timetable_for_day( timetable, day )
      timetable.select{ |t| t.tt_day_of_week == day }
               .sort_by{ |e| e[:tt_number_of_lesson] }
    end

    # Return russian name for type of lesson.
    def collect_types_of_lesson
      [ ["Обязательно занятие", "Primary lesson"], ["Электив", "Extra"] ]
    end

    # Check if timetable already has been created for school class.
    def timetable_exists?( school_class )
      not timetable_for_class( school_class ).empty?
    end
end

My view

<%= form_for @tt, :html => {:class => "form-horizontal"} do |f| %>
  <%= field_set_tag do %>

    <%= f.hidden_field :tt_number_of_lesson %>
    <%= f.hidden_field :tt_day_of_week %>
    <%= f.hidden_field :school_class_id %>

    <div class="control-group">
       <%= f.label :tt_day_of_week, "Day of the week", :class => "control-label" %>

       <div class="controls">
         <%= content_tag( :span, translate_day_of_week( @tt.tt_day_of_week ),
                          :class =>"input-xlarge uneditable-input span2" ) %>
       </div>
    </div>

    <div class="control-group">
       <%= f.label :tt_number_of_lesson, "Number of lesson", :class => "control-label" %>

       <div class="controls">
         <%= content_tag( :span, @tt.tt_number_of_lesson,
                          :class =>"input-xlarge uneditable-input span1" ) %>
       </div>
    </div>

    <hr/>

    <div class="control-group">
      <%= f.label :curriculum_id, "Type of subject", :class => "control-label" %>

      <div class="controls">
        <%= f.select( :curriculum_id,                                                           
                      options_for_select( @subjects_with_curriculums,
                                          @tt.curriculum_id ),
                      { :include_blank => true }) %>
      </div>
    </div>

    <div class="control-group">
      <%= f.label :tt_room, "Code of the room", :class => "control-label" %>

      <div class="controls">
        <%= f.text_field :tt_room, :class => "span2", :maxlength => 3 %>
      </div>
    </div>

    <div class="control-group">
      <%= f.label :tt_type, "Type of the lesson", :class => "control-label" %>

      <div class="controls">
        <%= f.select( :tt_type,                                                           
                      options_for_select( @types_of_lesson,
                                          @tt.tt_type ),
                      { :include_blank => true }) %>
      </div>
    </div>

    <%= f.submit "Update", :class => "btn btn-large btn-warning" %>
  <% end %>
<% end %>  

When i remove:

<div class="control-group">
  <%= f.label :curriculum_id, "Type of subject", :class => "control-label" %>

  <div class="controls">
    <%= f.select( :curriculum_id,                                                           
                  options_for_select( @subjects_with_curriculums,
                                      @tt.curriculum_id ),
                  { :include_blank => true }) %>
  </div>
</div>

<div class="control-group">
  <%= f.label :tt_type, "Type of the lesson", :class => "control-label" %>

  <div class="controls">
    <%= f.select( :tt_type,                                                           
                  options_for_select( @types_of_lesson,
                                      @tt.tt_type ),
                  { :include_blank => true }) %>
  </div>
</div>

I can view my test error. I can’t figure what is going on.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:19:41+00:00Added an answer on June 5, 2026 at 7:19 am

    You say this in your update controller:

    render 'edit'
    

    That simply tells Rails to render the edit template, it doesn’t run any of the code associated with the edit controller, it simply renders edit.html.erb in the current context.

    From the Layouts and Rendering in Rails Guide:

    2.2.2 Rendering an Action’s View
    [some stuff about render 'x', render :x, and render :action => :x being the same…]

    Using render with :action is a frequent source of confusion for Rails newcomers. The specified action is used to determine which view to render, but Rails does not run any of the code for that action in the controller. Any instance variables that you require in the view must be set up in the current action before calling render.

    Pay particular attention to the last sentence:

    Any instance variables that you require in the view must be set up in the current action before calling render.

    You’re setting @subjects_with_curriculums in the edit controller but using the edit view from the update controller. The result is that @subjects_with_curriculums is nil when edit.html.erb tries to use it when update tries to render the edit view.

    You’ll need to set @subjects_with_curriculums in your update controller, you might need @types_of_lesson as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I met a problem which is very strange, my company uses Visual Source Safe
I'm applying UnitTest just for a while, and today I met something very strange.
I met a very strange problem recently. I want to control the ListBoxItem's render
i met a very strange PHP behaviour, i don't understand why it behaves like
I met a strange problem. If I use for loop to render html into
I met a problem when trying @Singleton of Guice: import com.google.inject.Singleton; @Singleton public class
I met a strange error after migration to XTEXT2. When I write three elements,
I have run into a strange to me problem. For some reason multiple ||
I have met several cases where people computing reciprocal of a number with very
I have met a problem with virtualbox, I hope someone could help me here,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.