Rails 以未知方式未定义的方法
Rails Undefined Method in an unknown way
我克隆了一个 运行 很好的项目。我做了 bundle install
、bundle update
、db:create
、db:migrate
等等。
现在当我运行http://localhost:3000/orders/new
它returns一个NoMethodError in Admin::Orders#new
我在 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
这是我的 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'
在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' %>
和_form.html.erb
下的这个有错误
<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>
错误是这一行:<%= @nextShiftTime=format_datetime(@user.admin_areas.next_shift_time_from_now) %>
哪个returns...
nil:NilClass
的未定义方法“next_shift_time_from_now”
我必须配置什么来解决这个问题吗?我认为代码没有任何问题,因为它以前和最近的克隆都运行良好,我可能添加的代码没有任何变化,程序导致了这个错误。
如果行
@nextShiftTime=format_datetime(@user.admin_areas.next_shift_time_from_now)
returns一个undefined method 'next_shift_time_from_now' for nil:NilClass
错误,那么唯一可能的解释就是@user.admin_areas
returnsnil
.
查看您的数据库:您至少有一个 Admin::Account
没有匹配的 Admin::Area
。
我克隆了一个 运行 很好的项目。我做了 bundle install
、bundle update
、db:create
、db:migrate
等等。
现在当我运行http://localhost:3000/orders/new
它returns一个NoMethodError in Admin::Orders#new
我在 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
这是我的 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'
在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' %>
和_form.html.erb
下的这个有错误
<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>
错误是这一行:<%= @nextShiftTime=format_datetime(@user.admin_areas.next_shift_time_from_now) %>
哪个returns... nil:NilClass
的未定义方法“next_shift_time_from_now”我必须配置什么来解决这个问题吗?我认为代码没有任何问题,因为它以前和最近的克隆都运行良好,我可能添加的代码没有任何变化,程序导致了这个错误。
如果行
@nextShiftTime=format_datetime(@user.admin_areas.next_shift_time_from_now)
returns一个undefined method 'next_shift_time_from_now' for nil:NilClass
错误,那么唯一可能的解释就是@user.admin_areas
returnsnil
.
查看您的数据库:您至少有一个 Admin::Account
没有匹配的 Admin::Area
。