在rails中进行验证后维护表单中的数据

时间:2017-04-25 16:31:07

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

我正在验证我的表单,但发送错误消息会清除字段,我希望保留数据,以便我只完成我需要的那些

我在控制器中有这个代码:

def create
mensaje=""
@farm = Farm.new(params[:farm])
if @farm.nombre==""  || !valid_prescence?(@farm.nombre)
  mensaje="Favor de capturar los datos que se encuentran como requeridos"
else
  @buscar=Farm.where(nombre: params[:farm][:nombre], tipo: params[:farm][:cliente_id])
  if @buscar.any?
    mensaje="La finca "+params[:farm][:nombre]+" ya se encuentra registrada en el sistema, favor de verificar."
  end
end

respond_to do |format|
  if mensaje !=""
    format.html { redirect_to new_farm_path, notice: mensaje }
    format.json { render json: @farm.errors, status: :unprocessable_entity }
  else
    if@farm.save
    format.html { redirect_to @farm, notice: 'Finca creada correctamente.' }
    format.json { render json: @farm, status: :created, location: @farm }
  else
    format.html { render action: "new" }
    format.json { render json: @farm.errors, status: :unprocessable_entity }
  end
end end end

在落入异常的那一刻,我将返回表单,但是空字段 我想要的是能够在发送消息后保留数据

This is part of the form after the validation

3 个答案:

答案 0 :(得分:0)

只要您将某些内容放入mensaje,响应内容就会重定向到new操作,这就是表单为空的原因。你必须清除控制器,那些验证不属于那里,我只为你做了一些工作。

def create
  @farm = Farm.new(params[:farm])
  if @farm.nombre == ""  || !valid_prescence?(@farm.nombre)
    flash[:notice] = "Favor de capturar los datos que se encuentran como requeridos"
  else
    @buscar = Farm.where(nombre: params[:farm][:nombre], tipo: params[:farm][:cliente_id])
    if @buscar.any?
      flash[:notice] = "La finca #{params[:farm][:nombre]} ya se encuentra registrada en el sistema, favor de verificar."
    end
  end

  respond_to do |format|
    if @farm.save
      format.html { redirect_to @farm, notice: 'Finca creada correctamente.' }
      format.json { render json: @farm, status: :created, location: @farm }
    else
      format.html { render action: "new" }
      format.json { render json: @farm.errors, status: :unprocessable_entity }
    end
  end
end

答案 1 :(得分:0)

答案 2 :(得分:0)

验证不属于控制器。将您的验证放在模型中(业务逻辑)。以下是有关验证的出色指南:http://guides.rubyonrails.org/active_record_validations.html

之后,您的控制器将减少并仅决定def create @farm = Farm.new(params[:farm]) respond_to do |format| if @farm.save format.html { redirect_to @farm, notice: 'Finca creada correctamente.' } format.json { render json: @farm, status: :created, location: @farm } else format.html { render action: "new" } format.json { render json: @farm.errors, status: :unprocessable_entity } end end end (已显示为https://stackoverflow.com/users/336392/thomas-r-koll)。

public class SomeModel extends RealmObject implements Parcelable {
  public long id;

  public RealmList<SomeOtherModel> otherModels;

  protected SomeModel(Parcel in) {
   id = in.readLong();
   //here i need to otherModel RealmList from parcel
  }

  @Override
  public void writeToParcel(Parcel dest, int flags) {
    dest.writeLong(id);
    // here i need to write the otherModels RealmList
  }
  //other methods omitted 
}

在视图中查看表单代码也很有趣。您应该知道所有值以及验证中的错误消息都在@farm实例变量中。如果您正确地编写表单(即遵循标准的Rails约定),它将自动显示错误和旧值。