【问题标题】:rails 3.2 NoMethodError undefined method `slice' for nil:NilClassrails 3.2 NoMethodError undefined method `slice' for nil:NilClass
【发布时间】:2013-08-26 07:52:19
【问题描述】:

我得到这个错误结束不知道如何解决它。奇怪的是,它以前工作过。我想在我运行注释之后,它坏了,但不确定。 错误来自 confs.controller 索引和自己的方法。 它也拒绝这样的事情: conf.machine_brand[0,1].upcase as NoMethodError [ ] bla bla 这是我的 conf 模型:

# == Schema Information
#
# Table name: confs
#
#  id                 :integer          not null, primary key
#  machine_brand      :string(255)
#  machine_model      :string(255)
#  control_unit_brand :string(255)
#  control_unit_model :string(255)
#  tool_axis_x        :decimal(, )
#  tool_axis_y        :decimal(, )
#  tool_axis_z        :decimal(, )
#  rotary_axis_number :integer
#  linear_axis_number :integer
#  turning_mode       :boolean
#  milling_mode       :boolean
#  description        :text
#  xml                :text
#  user_id            :integer
#  developer_id       :integer
#  created_at         :datetime         not null
#  updated_at         :datetime         not null
#

class Conf < ActiveRecord::Base
   attr_accessible :linear_axis_number, :control_unit_brand, :control_unit_model, :description, :developer_id, :machine_brand, :machine_model, :milling_mode, :rotary_axis_number, :tool_axis_x, :tool_axis_y, :tool_axis_z, :turning_mode, :user_id, :xml 

   belongs_to :developer, :class_name => 'User', :foreign_key => 'developer_id'
   belongs_to :receiver, :class_name => 'User', :foreign_key => 'user_id'

   validates :user_id, presence: true
   validates :developer_id, presence: true
end

这是 confs.controller:

class ConfsController < ApplicationController
before_filter  :signed_in_user, only:[:index, :edit, :update, :destroy]
before_filter  :developer_user, only: :destroy

def new
  @conf = Conf.new
end

def index
  @grouped = {}
  Conf.all.each do |conf|
    letter = conf.machine_brand.slice(0,1).upcase
    @grouped[letter] ||= []
    @grouped[letter] << conf
end
end

def show
@conf = Conf.find(params[:id])

respond_to do |format|
    format.html #index.html.erb
    format.json { render json: @conf }
    format.xml { render xml: @conf }  
  end
end

def own
  @grouped = {}
  Conf.where(:developer_id => current_user.id).each do |conf|
    letter = conf.machine_brand.slice(0,1).upcase
    @grouped[letter] ||= []
    @grouped[letter] << conf
  end
end

def create
@conf = Conf.new(conf_params)

  if @conf.save
    flash[:success] = "New Configuration uploaded!"
    redirect_to conf_show_path
  else
    flash[:error] = "There is a problem!"
    render 'new'
  end
end

def destroy
  @conf = Conf.find(params[:id]).destroy
  redirect_to conf_show_own_path
end

def update
  @conf.update_attributes(params[:conf])  
end

private

def signed_in_user
  unless signed_in?
    store_location
    redirect_to signin_url, notice: "Please sign in"    
  end
end

def admin_user
  redirect_to(root_path) unless current_user.admin?
end

def developer_user
  redirect_to(root_path) unless current_user.developer?
end

def conf_params
  params.require(:conf).permit(:xml, :user_id, :developer_id) if params[:conf]
end

end

如果你愿意,这是 conf.new:

<% provide(:title, 'New Configuration')%>
<h1> Upload new configuration </h1>

<div class="row">
  <div class="span6 offset3">

     <%= form_for @conf, :html => {:multipart => true} do |f| %>

    <%= f.label :machine_brand %>
    <%= f.text_field :machine_brand %>

    <%= f.label :machine_model %>
    <%= f.text_field :machine_model %>

    <%= f.label :control_unit_brand %>
    <%= f.text_field :control_unit_brand %>

    <%= f.label :control_unit_model %>
    <%= f.text_field :control_unit_model %>

    <%= f.label :tool_axis_x %>
    <%= f.text_field :tool_axis_x %>

    <%= f.label :tool_axis_y %>
    <%= f.text_field :tool_axis_y %>

    <%= f.label :tool_axis_z %>
    <%= f.text_field :tool_axis_z %>

    <%= f.label :rotary_axis_number %>
    <%= f.text_field :rotary_axis_number %>

    <%= f.label :linear_axis_number %>
    <%= f.text_field :linear_axis_number %>

    <%= f.label :turning_mode %>
    <%= f.text_field :turning_mode %>

    <%= f.label :milling_mode %>
    <%= f.text_field :milling_mode %>

    <%= f.label :description %>
    <%= f.text_field :description %>

    <%= f.label :xml %>
    <%= f.text_field :xml %>

    <%= f.label :client %>
    <%= f.collection_select :user_id, User.where(:admin => false, :developer => false), :id, :name, options ={:prompt => "Select a client"}, :class =>"user" %>

    <%= f.label :me %>
    <%= f.collection_select :developer_id, User.where(:id => current_user.id), :id, :name, options ={:prompt => "Select me"}, :class =>"user" %>

<br />
    <%= f.submit "Upload", class: "btn btn-large btn-primary" %>
<% end %>
  </div>
</div>

【问题讨论】:

  • 如果这是导致它的行letter = conf.machine_brand.slice(0,1).upcase,则意味着返回的 conf.machine_brand 为 nil 且未设置。如果必须设置,则需要在模型中进行验证,以便在没有机器品牌的情况下无法创建它。
  • conf.machine_brand.slice(0,1) 我认为您在这里遇到了错误,因为您在 'Conf` 和 machine_brand 之间没有任何关联,所以只需在您的控制器中执行 letter = params[:machine_brand].to_s.slice(0,1).upcaseletter = params[:conf][:machine_brand].to_s.slice(0,1).upcase
  • 我认为某些行的 conf.machine_brand 可能为空。检查数据库中的 conf.machine_brand 是否为空。或者在 IRC Conf.all 中
  • 实际上,Conf.all.group_by{|conf| conf.machine_brand? ? conf.machine_brand[0].upcase : 'N/A' } 应该能得到你想要的东西
  • 另外,我认为您不应该在启用 AR 批量分配保护白名单的情况下使用 strong_parameters。而且您只允许 :xml, :user_id, :developer_id 在您的 conf_params 中,其余属性在您的模型中将简单地为零......

标签: ruby-on-rails ruby-on-rails-3.2 slice nomethoderror


【解决方案1】:

conf.machine_brand.slice(0,1)
我认为您在这里遇到了错误machine_brand 所以只需在您的控制器中执行
letter = params[:machine_brand].to_s.slice(0,1).upcase unless params[:machine_brand].blank?

letter = params[:conf][:machine_brand].to_s.slice(0,1).upcase unless params[:machine_brand].blank?

【讨论】:

  • 他怎么没有 Conf 和 machine_brand 之间的关联?它在他的 attr_accessible 中,在他的形式中
  • @Althaf 现在看看我的帖子
  • 我将所有属性添加到conf_param并验证了它们,所以我的问题解决了,非常感谢大家
  • 你能投票吗...@kalahari 谢谢...让我投票赞成你的问题
  • 它说我未满 15 岁,不能投票 :( 我是新人,声誉如何提高?
【解决方案2】:

正如@Rajarshi 提到的,错误在以下代码中

conf.machine_brand.slice(0,1).upcase

错误表明您在 nil 对象上调用 slice,这意味着您的 conf 记录之一的 machine_brand 为零。我不确定你想如何解决这个问题,但是如果你添加一个需要 machine_brand 的验证,这个问题将会减少

class Conf < ActiveRecord::Base
  ...
  validates :machine_brand, presence: true

或者您只能获取存在machine_brand 的记录

Conf.where('machine_brand IS NOT NULL').each do |conf|
  conf.machine_branch.slice(...)

【讨论】:

  • 他正在发送 machine_brand 通过参数查看他的视图文件
  • 这可能是真的,但这不会阻止通过其他方式创建带有 null machine_brand 的 conf(s)。
  • @RajarshiDas 注意他的conf_param 中没有:machine_brand
  • 我将所有属性添加到conf_param并验证了它们,所以我的问题解决了,非常感谢大家
猜你喜欢
  • 1970-01-01
  • 2015-08-02
  • 2020-09-24
  • 2015-03-19
  • 2016-08-13
  • 1970-01-01
  • 2017-07-04
  • 2016-01-02
  • 2012-08-08
相关资源
最近更新 更多