【问题标题】:Adding to has_one if text_field != blank or nil如果 text_field != 空白或 nil,则添加到 has_one
【发布时间】:2013-02-24 22:52:31
【问题描述】:

我有一个简单的 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 中的空白,我只是不希望将记录添加到损坏表中..

【问题讨论】:

  • 我们需要为该表单查看您的控制器。
  • 这里有些东西没有意义。我是否正确理解您希望您的 Packjob 能够拥有一个 Damage 对象,但您不希望该对象出现在损害表中,除非它具有非零和非空文本字段?您可以为内存中的Packjobs 执行此操作,但是一旦您想将其存储在数据库中,您将不得不更改您的架构以获得此效果。

标签: ruby-on-rails ruby-on-rails-3 forms


【解决方案1】:

这是validations 的工作。具体来说,您需要两件事:

  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 字段不能为空的限制。请参阅the migrations guide 了解更多信息。

【讨论】:

  • 不会强制使用 has_one 吗?我认为问题在于他是在控制器与建筑物中创建,我假设有时他不想创建 has_on 关系。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    相关资源
    最近更新 更多