-1

我有一个简单的 has_one 关系设置,但我只想在 text_field 不是空白或零时将记录添加到 Damages 表。现在它正在将空白或 nil 记录添加到另一个表中。

我的观点:

  <%= f.fields_for :damage do |builder| %>    
        <%= builder.label 'Damage' %><br />
        <%= builder.text_field :dam_detail %> 
  <% end %>

打包作业型号:

class Packjob < ActiveRecord::Base
   attr_accessible :pj_damage
   has_one :damage
   accepts_nested_attributes_for :damage
end

损坏模型:

 class Damage < ActiveRecord::Base
    attr_accessible :dam_detail
    belongs_to :packjob
end

如何只允许添加非空白或零值?将此逻辑添加到帮助程序的最佳做法是什么?

编辑:

这是 Packjobs 的控制器:

class PackjobsController < ApplicationController
  # GET /packjobs
  # GET /packjobs.json
  def index
    @packjobs = Packjob.includes(:damage).all
    @packers = Packer.find(:all)
    @rigs = Rig.find(:all, :order => "rig_type_number")


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

  # GET /packjobs/1
  # GET /packjobs/1.json
  def show
    @packjob = Packjob.find(params[:id])
    @packers = Packer.find(:all)

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

  # GET /packjobs/new
  # GET /packjobs/new.json
  def new
    @packjob = Packjob.new
    @packers = Packer.find(:all, :conditions => { :p_team => "t" }, :order => "p_name")
    @rigs = Rig.find(:all, :conditions => { :rig_status => "t" }, :order => "rig_type_number")
    @damage = @packjob.build_damage             
    #@book = @author.build_book    

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

  # GET /packjobs/1/edit
  def edit
    @packjob = Packjob.find(params[:id])
    @packers = Packer.find(:all, :conditions => { :p_team => "t" }, :order => "p_name")
    @rigs = Rig.find(:all, :conditions => { :rig_status => "t" }, :order => "rig_type_number")
  end

  # POST /packjobs
  # POST /packjobs.json
  def create
    @packjob = Packjob.new(params[:packjob])
    @packers = Packer.find(:all, :conditions => { :p_team => "t" }, :order => "p_name")
    @rigs = Rig.find(:all, :conditions => { :rig_status => "t" }, :order => "rig_type_number")

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

  # PUT /packjobs/1
  # PUT /packjobs/1.json
  def update
    @packjob = Packjob.find(params[:id])
    @packers = Packer.find(:all, :conditions => { :p_team => "t" }, :order => "p_name")
    @rigs = Rig.find(:all, :conditions => { :rig_status => "t" }, :order => "rig_type_number")

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

  # DELETE /packjobs/1
  # DELETE /packjobs/1.json
  def destroy
    @packjob = Packjob.find(params[:id])
    @packjob.destroy

    respond_to do |format|
      format.html { redirect_to packjobs_url }
      format.json { head :no_content }
    end
  end
end

另外,我希望 packjob 允许损坏 text_field 中的空白,我只是不希望将记录添加到损坏表中。

4

2 回答 2

2

这是一项验证工作。具体来说,您需要两件事:

  1. 您希望您的Damage班级验证它:dam_detail不是空白或nil

    class Damage < ActiveRecord::Base
      # ... rest of class here ...
      validates :dam_detail, :presence => true, :length => { :minimum => 1 }
    end
    
  2. 您希望您的Packjob类验证其包含的Damage对象是否有效:

    class Packjob < ActiveRecord::Base
      # ... rest of class here ...
      validates_associated :damage
    end
    

我还建议修改您的数据库架构以添加dam_detail字段不能为空的限制。有关更多信息,请参阅迁移指南

于 2013-02-24T23:02:00.990 回答
0

尽管它更常用于 has_many,但茧宝石https://github.com/nathanvda/cocoon对此非常有用。该 gem 将允许您从前端动态建立关系。它也将允许您破坏这种关系。

于 2013-02-24T23:12:20.280 回答