Ruby on rails rails 3.2命名错误未定义方法'slice';零级:零级
我得到了这个错误结束没有线索如何修复它。奇怪的是,它以前是工作的。我想在我运行注释后,它已损坏,但不确定。 错误来自confs.controller索引和自己的方法。 它还拒绝这样的内容:conf.machine_brand[0,1]。upcase作为NoMethodError[]bla bla bla 这是我的conf模型:Ruby on rails rails 3.2命名错误未定义方法'slice';零级:零级,ruby-on-rails,ruby-on-rails-3.2,slice,nomethoderror,Ruby On Rails,Ruby On Rails 3.2,Slice,Nomethoderror,我得到了这个错误结束没有线索如何修复它。奇怪的是,它以前是工作的。我想在我运行注释后,它已损坏,但不确定。 错误来自confs.controller索引和自己的方法。 它还拒绝这样的内容:conf.machine_brand[0,1]。upcase作为NoMethodError[]bla bla bla 这是我的conf模型: # == Schema Information # # Table name: confs # # id :integer
# == 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
#
#id:整数不为空,主键
#机器品牌:字符串(255)
#机器型号:字符串(255)
#控制单元品牌:字符串(255)
#控制单元模型:字符串(255)
#刀具轴:十进制(,)
#刀具轴:十进制(,)
#刀具轴:十进制(,)
#旋转轴编号:整数
#线性轴数:整数
#旋转模式:布尔型
#铣削模式:布尔型
#说明:文本
#xml:文本
#用户id:integer
#开发者id:整数
#创建时间:datetime非空
#更新时间:datetime非空
#
类Conf'User',:外键=>'developer\u id'
属于:接收者,:类名称=>'User',:外键=>'User\u id'
验证:用户id,状态:true
验证:开发者id,状态:true
结束
这是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
class ConfsController
false,:developer=>false),:id,:name,options={:prompt=>“选择客户端”},:class=>“用户”%>
当前_user.id),:id,:name,options={:prompt=>“Select me”},:class=>“user”%>
conf.machine\u brand.slice(0,1)
我认为您在这里得到了错误
machine\u brand
,因此只需在控制器中执行letter=params[:machine\u brand]。to_.slice(0,1)。除非params[:machine\u brand]为空,否则为大写?
或
letter=params[:conf][:machine\u brand]。to_s.slice(0,1)。除非params[:machine\u brand]为空,否则为大写?
conf.machine\u brand.slice(0,1)
我认为您在这里得到了错误
machine\u brand
,因此只需在控制器中执行letter=params[:machine\u brand]。to_.slice(0,1)。除非params[:machine\u brand]为空,否则为大写?
或
letter=params[:conf][:machine_brand]。to_s.slice(0,1)。除非params[:machine_brand]为空,否则为空?
正如@Rajarshi提到的,错误在以下代码中
conf.machine_brand.slice(0,1).upcase
错误表明您正在对nil
对象调用slice
,这意味着您的一条conf记录具有nil machine\u品牌。我不确定您希望如何处理此问题,但如果您添加需要机器品牌的验证,则此问题将减少
class Conf < ActiveRecord::Base
...
validates :machine_brand, presence: true
正如@Rajarshi提到的,错误在下面的代码中
conf.machine_brand.slice(0,1).upcase
错误表明您正在对nil
对象调用slice
,这意味着您的一条conf记录具有nil machine\u品牌。我不确定您希望如何处理此问题,但如果您添加需要机器品牌的验证,则此问题将减少
class Conf < ActiveRecord::Base
...
validates :machine_brand, presence: true
如果这是导致它的行
letter=conf.machine\u brand.slice(0,1).upcase
,则表示返回的conf.machine\u brand为零且未被设置。如果必须设置,您需要在模型中进行验证,以便在没有机器品牌的情况下无法创建该模型。conf.machine\u brand.slice(0,1)
我认为您在这里遇到了错误,因为“conf”和machine\u brand
之间没有任何关联,所以只需在控制器中执行letter=params[:machine\u brand].to_.slice(0,1).upcase
或letter=params[:conf][:machine_brand].to_.slice(0,1).upcase
我认为conf.machine_brand对于某些行可能为空。检查数据库中conf.machine_brand是否为空。或者在IRC Conf.allal中,Conf.all.group_by{Conf | Conf.machine_brand→Conf.machine_brand[0]。upcase:'N/A'}
应该完全满足您的需求。所以,我认为您不应该在AR质量分配保护白名单打开的情况下使用strong_参数。您只允许:xml、:user\u id、:developer\u id
在您的conf\u参数中,其余的属性在您的模型中将仅仅是nil…如果这是导致它的行letter=conf.machine\u brand.slice(0,1).upcase
则意味着返回的conf.machine\u品牌为nil,并且没有被设置。如果必须设置,您需要在模型中进行验证,以便在没有机器品牌的情况下无法创建该模型。conf.machine\u brand.slice(0,1)
我认为您在这里遇到了错误,因为“conf”和machine\u brand
之间没有任何关联,所以只需在控制器中执行letter=params[:machine\u brand].to_.slice(0,1).upcase
或letter=params[:conf][:machine_brand].to_.slice(0,1).upcase
我认为conf.machine_brand对于某些行可能为空。检查数据库中conf.machine_brand是否为空。或者在IRC Conf.allal中,Conf.all.groupu by{Conf|c