开发者

Form validation, two models, one form, DubleRenderError

I have report_controller where I have two objects, @report and @reporte, and I want to save both objects in database. That works without validation, but when I put validation in models, I get

AbstractController::DoubleRenderError in ReportController#encreate

I using exportnew action to get view, and encreate to save this form.

Where I make mistake?

ReportController:

class ReportController < ApplicationController
  before_filter :authenticate_user!
  before_filter :load
  layout "application"

  def load
    @company = Company.find_by_id(current_user.company_id)
    @date = Date.today
    @report = ReportMain.new
    @reporte = ReportE.new
  end

  def ind开发者_StackOverflowex
    list
    render("list")
  end

  def list
    @reports = ReportMain.all
  end

  def exportnew
    render("ennew")
  end

  def encreate

    @report = ReportMain.new
    @reporte = ReportE.new
    @reportparam = params[:report_main]


    @report.waste_id = params[:waste][:code]
    @report.warehouse_id = Warehouse.find_by_user_id(current_user.id).id
    @report.user_id = current_user.id
    @report.company_id = current_user.company_id
    @report.amount = @reportparam[:amount]
    @report.isimport = false
    @report.isfinished = false
    @report.reportnumber =  ReportMain.where(:company_id => current_user.company_id).count.to_i+1
    if @report.save
      @reporte.report_main_id = @report.id
    else
      redirect_to(:action => 'exporttoxicnew')
    end

    @reporte.vrstaotpada = params[:vrstaotpada]
    @reporte.nacinpakovanja = params[:nacinpakovanja]
    @reporte.ispitivanjebroj = @reportparam[:ispitivanjebroj]
    @reporte.datumispitivanja = @reportparam[:datumispitivanja]
    @reporte.q_pripadnost = @reportparam[:q_pripadnost]
    @reporte.datumpredaje = @date

    if @reporte.save
      redirect_to(:action => 'show', :id => @reporte.id)
      flash[:notice] = "Izveštaj je uspešno kreiran."
    else
      redirect_to(:action => 'exporttoxicnew')
    end
  end



  def show
    @report = ReportMain.find(params[:id])
    @warehouse = @report.warehouse.name
  end

end

ReportMain model:

class ReportMain < ActiveRecord::Base
  has_many :report_es
  has_many :report_is
  belongs_to :user
  belongs_to :company
  belongs_to :warehouse
  belongs_to :waste

  validates_presence_of :waste_id
  validates_presence_of :amount
  validates_length_of :amount, :within => 1..255

end

ReportE model:

class ReportE < ActiveRecord::Base
  belongs_to :report_main

  validates_presence_of :vrstaotpada
  validates_presence_of :nacinpakovanja
  validates_presence_of :ispitivanjebroj
  validates_presence_of :datumispitivanja

  validates_length_of :vrstaotpada, :within => 3..255
  validates_length_of :nacinpakovanja, :within => 3..255
  validates_length_of :ispitivanjebroj, :within => 3..255
  validates_length_of :datumispitivanja, :within => 3..255

end

View starts with (it's huge HTML):

<%= form_for(:report_main, :url => {:action => 'encreate'}) do |f| %>


You need to change:

if @report.save
  @reporte.report_main_id = @report.id
else
  redirect_to(:action => 'exporttoxicnew')
end

into:

if @report.save
  @reporte.report_main_id = @report.id
else
  redirect_to(:action => 'exporttoxicnew')
  return
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜