首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails 3.1限制子模型的数量

Rails 3.1限制子模型的数量
EN

Stack Overflow用户
提问于 2011-11-17 15:19:28
回答 1查看 362关注 0票数 0

大家好,我有两个模型:用户(又名家长)和普罗非(又名子).And,我想限制一个用户的profil数量为一个。

models/user.rb

代码语言:javascript
复制
class User < ActiveRecord::Base

  has_one :profil

end

models/profil.rb

代码语言:javascript
复制
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时,我会得到以下错误消息:

代码语言:javascript
复制
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  

控制器/轮廓控制器

代码语言:javascript
复制
# -*- 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

我做错什么了?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-17 17:05:15

看看limit_profil_to_one中的第2行-- self.user是0,所以它失败了。

代码语言:javascript
复制
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?我假设您实际上是指概要文件),您应该将控制器中的用户设置为它应该是的用户,如下所示。

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8169589

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档