2011-11-17 85 views
0

好傢伙數目我有2種型號:用戶(又名父)和PROFIL(又名子)。而我想限制PROFIL數量的用戶之一。的Rails 3.1的限制兒童模特

型號/ user.rb

class User < ActiveRecord::Base 

    has_one :profil 

end 

型號/ profil.rb

class Profil < ActiveRecord::Base 
    attr_accessible :logo 
    belongs_to :user 
    mount_uploader :logo, ImageUploader 


    validate :limit_profil_to_one, :on => :create 

    def limit_profil_to_one 
     if self.user.profils(:reload).count > 1 
     errors.add(:base, "Exceeded thing limit") 
     end 
    end 
end 

但是當我嘗試創建一個PROFIL我收到以下錯誤信息:

NoMethodError (undefined method `profils' for nil:NilClass): 
    app/models/profil.rb:11:in `limit_profil_to_one' 
    app/controllers/profils_controller.rb:52:in `block in create' 
    app/controllers/profils_controller.rb:51:in `create 

controllers/p rofils_controller.rb

# -*- encoding : utf-8 -*- 

class ProfilsController < ApplicationController 


    # GET /factures 
    # GET /factures.json 
    def index 
    @profils = Profil.all 

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

    # GET /profils/1 
    # GET /profils/1.json 
    def show 
    @profil = Profil.find(params[:id]) 

    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @profil } 
    end 
    end 

    # GET /profils/new 
    # GET /profils/new.json 
    def new 
    @profil = Profil.new 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @profil } 
    end 
    end 

    # GET /profils/1/edit 
    def edit 
    @profil = Profil.find(params[:id]) 
    end 

    # POST /profils 
    # POST /profils.json 
    def create 
    @profil = Profil.new(params[:profil]) 

    respond_to do |format| 
     if @profil.save 
     format.html { redirect_to @profil, notice: 'Profil was successfully created.' } 
     format.json { render json: @profil, status: :created, location: @profil } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @profil.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PUT /profils/1 
    # PUT /profils/1.json 
    def update 
    @profil = Profil.find(params[:id]) 

    respond_to do |format| 
     if @profil.update_attributes(params[:profil]) 
     format.html { redirect_to @profil, notice: 'Profil was successfully updated.' } 
     format.json { head :ok } 
     else 
     format.html { render action: "edit" } 
     format.json { render json: @profil.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /factures/1 
    # DELETE /factures/1.json 
    def destroy 
    @profil = Profil.find(params[:id]) 
    @profil.destroy 

    respond_to do |format| 
     format.html { redirect_to profils_url } 
     format.json { head :ok } 
    end 
    end 
end 

我在做什麼錯了?

+0

我們需要看到您的控制器,請 –

+0

我這樣做已經但是你是有「異型材」注意,​​但是這不是一個一對多(用於複數),insted的,這是一個HAS_ONE所以應該是「 profil'在這種情況下。 –

+0

心中已經改成了PROFIL,我仍然得到以下錯誤'未定義的方法「PROFIL」爲無:NilClass' – blawzoo

回答

1

看在limit_profil_to_one線2 - self.user是零,因此失敗。

def limit_profil_to_one 
    if self.user.profils(:reload).count > 1 # self.user is nil 
     errors.add(:base, "Exceeded thing limit") 
    end 
end 

我在做什麼你的應用做一些假設,但對於這個職位,我會認爲你的控制器具有在控制器中定義的當前用戶和您爲該用戶創建一個PROFIL(方:請注意,什麼是profil?我假設你實際上是指profile)你應該將控制器中的用戶設置爲它應該是的用戶,就像這樣。

def create 
    @profil = Profil.new(params[:profil]) 
    @profil.user = current_user # however you access the currently logged in user 

    respond_to do |format| 
     if @profil.save 
      format.html { redirect_to @profil, notice: 'Profil was successfully created.' } 
      format.json { render json: @profil, status: :created, location: @profil } 
     else 
      format.html { render action: "new" } 
      format.json { render json: @profil.errors, status: :unprocessable_entity } 
     end 
    end 
end 
+0

謝謝您的回答,但 – blawzoo

+0

感謝一些代碼將是有益的蘭森布里格斯你砍使it.I've標誌着你的答案 – blawzoo