2013-08-26 97 views
1

我得到這個錯誤結束沒有線索如何解決它。奇怪的是,它在工作之前。我認爲在我運行註釋後,它被破壞了,但不確定。 錯誤來自confs.controller索引和自己的方法。 也拒絕像這樣:conf.machine_brand [0,1]作爲.upcase NoMethodError []喇嘛喇嘛 這是我的conf型號:rails 3.2 NoMethodError未定義的方法`slice'爲零:NilClass

# == 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> 
+0

如果這是導致它'letter = conf.machine_brand.slice(0,1).upcase'的那一行,那麼這意味着返回的conf.machine_brand是零且沒有被設置。如果必須設置,則需要在模型中進行驗證,以便在沒有機器品牌的情況下創建。 –

+0

'conf.machine_brand.slice(0,1)'我認爲你在這裏得到的錯誤是因爲你沒有'Conf'和'machine_brand'之間的任何關聯,所以在你的控制器中''letter = params [:machine_brand]。 to_s.slice(0,1).upcase'或'letter = params [:conf] [:machine_brand] .to_s.slice(0,1).upcase' –

+0

我認爲conf.machine_brand對於某些行可能是空的。檢查數據庫中的conf.machine_brand是否爲空。或者在IRC Conf.all –

回答

1

conf.machine_brand.slice(0,1)
我覺得你得到了ER ROR這裏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?

+0

Conf和machine_brand之間沒有關係?這是在他的attr_accessible和它的形式 –

+0

@Althaf現在看我的帖子 –

+0

我將所有屬性添加到conf_param並驗證它們,所以我的問題解決了,非常感謝你們所有人 – kalahari

0

正如@Rajarshi提到的,​​錯誤是在下面的代碼

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

錯誤說,你打電話slicenil對象上,這意味着您的一個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(...) 
+0

他通過參數發送machine_brand看看他的觀點文件 –

+0

可能是真實的,但不會阻止配置(S)通過其他方式創建空machine_brand。 – jvnill

+0

@RajarshiDas注意到他怎麼沒有':machine_brand'在他的'conf_param' – j03w

相關問題