nil:NilClass 的未定义方法 '>=' -- 无法弄清楚这意味着什么



当用户登录时,主页操作会将用户重定向到new_status_update_path,他们将在其中看到提交新status_update的表单。

def home
 if user_signed_in?
  redirect_to new_status_update_path
 end 
end  

然后,status_update_controller中的新操作应简单地将status_update对象传递给视图窗体,以便可以对其进行操作。

状态更新控制器:

def new
  @status_update = current_user.status_update.build if user_signed_in?
end 

视图:

<div class="row">
<div class="span6 offset3">
<%= form_for(@status_update) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <%= f.label :weight %>
  <%= f.text_field :weight %>
  <%= f.label :bf_pct %>
  <%= f.text_field :bf_pct %>
  <%= f.submit "Post", class:"btn btn-large btn-primary" %> 
<% end %>

渲染错误:

NoMethodError in StatusUpdatesController#new
 undefined method `>=' for nil:NilClass
app/models/status_update.rb:36:in `default_values'
app/controllers/status_updates_controller.rb:10:in `new'

status_update.rb

class StatusUpdate < ActiveRecord::Base
   belongs_to :user
   after_initialize :default_values
   attr_accessible :current_weight,
               :current_bf_pct,
               :current_lbm,
               :current_fat_weight,
               :change_in_weight,
               :change_in_bf_pct,
               :change_in_lbm,
               :change_in_fat_weight,
               :total_weight_change,
               :total_bf_pct_change,
               :total_lbm_change,
               :total_fat_change,
               :previous_weight,
               :previous_bf_pct,
               :previous_lbm,
               :previous_fat_weight,
               :created_at
   validates :user_id, presence: true
   validates :current_bf_pct, presence: true,
                          numericality: true,
                          length: { minimum: 2, maximum:5 }  
   validates :current_weight, presence: true,
                          numericality: true,
                          length: { minimum: 2, maximum:5 } 
   validates :current_lbm, presence: true
   validates :current_fat_weight, presence: true                   
   def default_values      
     if self.current_bf_pct >= 0.5
       self.current_bf_pct /= 100
        if self.current_bf_pct <= 0.04
          self.current_fb_pct *= 100
        end 
     end
     self.current_fat_weight = self.current_weight * self.current_bf_pct
     self.current_lbm = self.current_weight - self.current_fat_weight
   end  
   def previous_status_update
     previous_status_update = user.status_update.where( "created_at < ? ", self.created_at ).first   
     if previous_status_update == nil
       return self
     else
       previous_status_update
     end
   end 
    default_scope order: 'status_updates.created_at DESC'
 end

感谢您的帮助!

看起来问题可能出在您使用after_initialize 上。如果我没记错的话,after_initialize是在创建新对象后调用的。此时,对象上的所有属性都将为零,因此,您将无法使用像 >= 这样的比较。

编辑:

您可以尝试使用 ||= 习惯用法设置值,仅当属性为 nil 时,该习惯用法才会设置属性的默认值。因此,如果属性为 nil,则类似 self.current_bf_pct ||= 0.5 会将current_bf_pct设置为 0.5,否则它将使用其当前值。

换句话说,您似乎正在尝试比较尚未设置的属性的值。

undefined method `>=' for nil:NilClass

这意味着您正在尝试对status_update文件(第 36 行)内的 nil 对象的实例使用 >= 运算符。根据错误。

检查以确保正确调用状态更新控制器,并且数据按预期流动。

相关内容

最新更新