Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 添加到有一个文本字段!=空白或无_Ruby On Rails_Ruby On Rails 3_Forms - Fatal编程技术网

Ruby on rails 添加到有一个文本字段!=空白或无

Ruby on rails 添加到有一个文本字段!=空白或无,ruby-on-rails,ruby-on-rails-3,forms,Ruby On Rails,Ruby On Rails 3,Forms,我有一个简单的has_one关系设置,但我只想在text_字段不为空或nil时将记录添加到损坏表中。现在它正在向另一个表中添加空白或零的记录 我的看法是: <%= f.fields_for :damage do |builder| %> <%= builder.label 'Damage' %><br /> <%= builder.text_field :dam_detail %> <% en

我有一个简单的has_one关系设置,但我只想在text_字段不为空或nil时将记录添加到损坏表中。现在它正在向另一个表中添加空白或零的记录

我的看法是:

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


Packjob模型:

class Packjob < ActiveRecord::Base
   attr_accessible :pj_damage
   has_one :damage
   accepts_nested_attributes_for :damage
end
class Packjob
损伤模型:

 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
class PackjobsController“钻机类型\编号”)
回应待办事项|格式|
format.html#index.html.erb
format.json{render json:@packjobs}
终止
终止
#获取/打包作业/1
#GET/packjobs/1.json
def秀
@packjob=packjob.find(参数[:id])
@封隔器=封隔器。查找(:全部)
回应待办事项|格式|
format.html#show.html.erb
format.json{render json:@packjob}
终止
终止
#获取/打包作业/新建
#GET/packjobs/new.json
def新
@packjob=packjob.new
@packers=Packer.find(:all,:conditions=>{:p_team=>“t”},:order=>“p_name”)
@rigs=Rig.find(:all,:conditions=>{:Rig\u status=>“t”},:order=>“Rig\u type\u number”)
@损坏=@packjob.build\u损坏
#@book=@author.build\u book
回应待办事项|格式|
format.html#new.html.erb
format.json{render json:@packjob}
终止
终止
#获取/打包作业/1/编辑
定义编辑
@packjob=packjob.find(参数[:id])
@packers=Packer.find(:all,:conditions=>{:p_team=>“t”},:order=>“p_name”)
@rigs=Rig.find(:all,:conditions=>{:Rig\u status=>“t”},:order=>“Rig\u type\u number”)
终止
#邮政/打包工作
#POST/packjobs.json
def创建
@packjob=packjob.new(参数[:packjob])
@packers=Packer.find(:all,:conditions=>{:p_team=>“t”},:order=>“p_name”)
@rigs=Rig.find(:all,:conditions=>{:Rig\u status=>“t”},:order=>“Rig\u type\u number”)
回应待办事项|格式|
如果@packjob.save
format.html{将_重定向到@packjob,注意:'packjob已成功创建。}
format.json{呈现json:@packjob,状态::已创建,位置:@packjob}
其他的
format.html{呈现操作:“新建”}
format.json{render json:@packjob.errors,status::unprocessable_entity}
终止
终止
终止
#放置/打包作业/1
#PUT/packjobs/1.json
def更新
@packjob=packjob.find(参数[:id])
@packers=Packer.find(:all,:conditions=>{:p_team=>“t”},:order=>“p_name”)
@rigs=Rig.find(:all,:conditions=>{:Rig\u status=>“t”},:order=>“Rig\u type\u number”)
回应待办事项|格式|
如果@packjob.update_属性(参数[:packjob])
format.html{将_重定向到@packjob,注意:'packjob已成功更新。}
format.json{head:no_content}
其他的
format.html{呈现操作:“编辑”}
format.json{render json:@packjob.errors,status::unprocessable_entity}
终止
终止
终止
#删除/packjobs/1
#删除/packjobs/1.json
def销毁
@packjob=packjob.find(参数[:id])
@packjob.destroy
回应待办事项|格式|
format.html{redirect_to packjobs_url}
format.json{head:no_content}
终止
终止
终止
另外,我希望packjob允许损坏文本_字段中出现空白,我只是不希望将记录添加到损坏表中。

这是一个用于的作业。具体来说,您需要两件事:

  • 您希望您的
    Damage
    类验证
    :dam\u detail
    是否为空或
    nil

    class Damage < ActiveRecord::Base
      # ... rest of class here ...
      validates :dam_detail, :presence => true, :length => { :minimum => 1 }
    end
    

  • 我还建议修改数据库模式,添加
    dam\u detail
    字段不能为空的限制。更多信息,请参见。

    尽管它在很多情况下更常用,但cocoon gem非常适合于此。该gem将允许您从前端动态构建关系。它也会让你破坏这种关系

    我们需要查看您的控制器的表单。这里有些不合理的地方。我是否正确理解您希望您的
    Packjob
    能够有一个
    Damage
    对象,但您不希望该对象出现在damamages表中,除非它有一个非零和非空的文本字段?你可以在内存
    Packjob
    s中执行此操作,但一旦你想将其存储在数据库中,你就必须改变你的体系结构以获得此效果。这是否会强制执行此操作?我认为问题在于,他是在控制者与建筑中创造的,我假设他有时不想创造has_on关系。
    class Packjob < ActiveRecord::Base
      # ... rest of class here ...
      validates_associated :damage
    end