0

I cloned a project that runs just fine. I did the bundle install, bundle update, db:create, db:migrate, and whatnot.

Now when I run http://localhost:3000/orders/new it returns a NoMethodError in Admin::Orders#new

I have this as part of my code under model/admin/area.rb

has_many :admin_accounts, :class_name => 'Admin::Account'

validates :name, :presence => true
validates :timezone, :presence => true
validates :time_offset, :presence => true
validates :shift_time1, :presence => true
validates :shift_time2, :presence => true
validates :unit_price_12, :presence => true
validates :unit_price_24, :presence => true

def next_shift_time_from_now
  current_time = Time.zone.now
  next_shift_time(current_time)
end

def next_shift_time(time)
  shift1 = time.change(hour: self[:shift_time1].hour)
  shift2 = time.change(hour: self[:shift_time2].hour)
  get_shift(time, shift1, shift2)
end

And this under my accounts.rb

require 'digest/md5'
before_save :encrypt_password
after_initialize :default_values

has_many :admin_orders, :class_name => 'Admin::Order'
belongs_to :admin_areas, :class_name => 'Admin::Area', :foreign_key => 'area_id'

under new.html

<div class="page-header clearfix">
  <div class="pull-right"><%= link_to 'Back', orders_path, class: "btn btn-primary btn-sm" %></div>
  <h3 class="pull-left">
    Creating New Order
  </h3>
</div>

<%= render 'form' %>

and this under _form.html.erb which has the error

    <div class="form-group field-bottom-container">
      <label id="nextShiftTime">Next Shift Time</label>

      <div>
        <% @user = Admin::Account.find($user_id) %>
        <%= @nextShiftTime=format_datetime(@user.admin_areas.next_shift_time_from_now) %>
      </div>
    </div>

The error is this line: <%= @nextShiftTime=format_datetime(@user.admin_areas.next_shift_time_from_now) %>

which returns... undefined method `next_shift_time_from_now' for nil:NilClass

Is there anything I must configure to fix this? I think there's nothing wrong with the code since it worked well previously and upon recent clone, with no changes with the codes i might add, the program is causing this error.

5
  • Since @user.admin_areas is nil, can you post the user model? Commented May 6, 2015 at 6:50
  • i have no user model. the models used are account and area. the admin_areas belongs to area.rb as well as the def next_shift_time_from_now @jphager2 the 'at' user gets the account model by the way Commented May 6, 2015 at 6:57
  • 1
    Ok got it. So first thing: belongs_to :admin_areas is plural. Is that intentional? Because you are only expecting one admin_area. Secondly, did you assign an area_id to the account in the database? I'm guessing @user.area_id == nil Commented May 6, 2015 at 7:03
  • The line <% @user = Admin::Account.find($user_id) %> violates at least three Rails' best practice pattern. Commented May 6, 2015 at 7:03
  • @jphager2 i just found out that area_id turned null in my database. thanks a lot!! that solved my problem Commented May 6, 2015 at 7:21

1 Answer 1

1

If the line

@nextShiftTime=format_datetime(@user.admin_areas.next_shift_time_from_now) 

returns an undefined method 'next_shift_time_from_now' for nil:NilClass error, then the only possible explanation is that @user.admin_areas returns nil.

Look into your database: You have at least one Admin::Account without a matching Admin::Area.

Sign up to request clarification or add additional context in comments.

2 Comments

what data should I add? the other clone of this same project went well with same codes... don't the versions of ruby/rails matter or what?
thanks... it helped me trace the problem. i just found out that my area_id is null in my database

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.