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