2012-07-22 58 views
1

嗨,我有這個問題運行spec文件。 這是reparator型號:「未定義的方法`元數據爲..」導軌

class Reparator < User 
    include Mongoid::Document 
    include Mongoid::Timestamps 
    field :private_reparator, :type => Boolean, :default => true 
    field :brand_name, :type => String 
    field :year_of_experience, :type => Integer, :default => 1 

    has_many :reparations 
    has_many :skills 

    validates_presence_of :skills, :year_of_experience 
    validates :year_of_experience, :numericality => {:greater_than_or_equal_to => 0} 
end 

這是技能模型:

class Skill 
    include Mongoid::Document 

    field :name, :type => String 

    belongs_to :reparator 

    validates_presence_of :name 
    validates_uniqueness_of :name 

end 

這是控制器:

class ReparatorsController < ApplicationController 
    respond_to :json 

    def index 
    @reparators = Reparator.all 
    respond_with @reparators 
    end 

    def show 
    @reparator = Reparator.find(params[:id]) 
    respond_with @reparator 
    end 

    def create 
    @reparator = Reparator.new(params[:reparator]) 
    @reparator.skills = params[:skills] 
    if @reparator.save 
     respond_with @reparator 
    else 
     respond_with @reparator.errors 
    end 
    end 

    def update 
    @reparator = Reparator.find(params[:id]) 

    if @reparator.update_attributes(params[:reparator]) 
     respond_with @reparator 
    else 
     respond_with @reparator.errors 
    end 
    end 

    def destroy 
    @reparator = Reparator.find(params[:id]) 
    @reparator.destroy 
    respond_with "Correctly destroyed" 
    end 

end 

這是針對該控制器規範文件(我我只是把沒有通過的測試):

it "Should create an reparator" do 
     valid_skills = [FactoryGirl.create(:skill).id, FactoryGirl.create(:skill).id] 
     valid_attributes = {:name => "Vianello", 
          :email => "[email protected]", 
          :address => "viale ciccio", 
          :private_reparator => true 
          } 
     post :create, :reparator => valid_attributes, :skills => valid_skills 
     assigns(:reparator).should be_a Reparator 
     assigns(:reparator).should be_persisted 
    end 

這是技能廠女孩:

FactoryGirl.define do 
    factory :skill do 
    sequence(:name) {|n| "skill#{n}"} 
    end 
end 
+0

它給了我這個錯誤:未定義的方法'ID爲「500d3e15670b4d0a32000027」:字符串 – Massimo 2012-07-23 12:02:41

回答

0

我認爲這是在您的規範中的一個錯字。改爲post :create, :reparator => valid_attributes, :skills => skills_ttributes應爲post :create, :reparator => valid_attributes, :skills => skills_attributes

+0

你是對的,我編輯它,這是一個錯字,現在我已經得到了一個不同的錯誤 – Massimo 2012-07-23 12:14:23

0

壞線是這一個

@reparator.skills = params[:skills] 

params[:skills]是字符串(已經傳遞的ID),但skills=方法期望被給予的實際Skill實例等鼓起的陣列。

除了skills=,mongoid還爲您提供了一個skill_ids=方法,它允許您通過只分配一個ID數組來更改關聯的對象。或者,加載技能對象你的自我,然後@reparator.skills = skills

+0

我已經嘗試過,但它告訴我未定義的方法'skill_ids'爲#我隨時調用FactoryGirl.create(關於skill_ids)「after_build {| reparator | reparator.skill_ids << FactoryGirl .create(:skill).id}「 – Massimo 2012-07-23 14:20:08

+0

該方法僅適用於mongoid 3,聽起來就像使用了mongoid 2,所以你只需要在沒有它的情況下做。 – 2012-07-23 16:18:52

+0

mmmm我不這麼認爲,我在別人的規格中使用它,但是用那個不工作! – Massimo 2012-07-23 18:26:19

相關問題