2011-11-17 5 views
0

こんにちは私は2つのモデルを持っています:ユーザー(別名親)とProfil(別名子)。ユーザーのprofilの数を1に制限します。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 

しかし、私はプロフィールを作成しようとすると、私は次のようなエラーメッセージが出ます:

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 

コントローラ/ 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

は、私はすでに、しかし、あなたが「profils」を持っていることに注意してくださいを参照する必要があるが、これは(複数の場合)多くのものではありませんその場合は「プロファイリング」します。 –

+0

私はそれをprofilに変更しましたが、私はまだ未定義のメソッド "profil" for nilを取得しました:NilClass' – blawzoo

答えて

1

limit_profil_to_oneの2行目を参照してください。 - self.userはnilなので失敗しています。

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

(私はあなたのアプリケーションが何をしているかについていくつかの仮定を作っていますが、この記事のために私はあなたのコントローラは、コントローラで定義されている現在のユーザーを持っていると仮定するつもりです、あなたはそのユーザーのプロフィールを作成していること私はあなたが実際にプロファイルを意味していると仮定しています)コントローラのユーザは、そのようになるはずのユーザに設定する必要があります。 「それがあるべきように、これはhas_oneのあるinsted

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

あなたがハックしてくれたことに感謝します。 – blawzoo

関連する問題