开发者

form_for, fields_for and two models

开发者 https://www.devze.com 2023-02-15 04:40 出处:网络
I have form that create two objects and save them to database. I want to do next things: save data in database (booth objects)

I have form that create two objects and save them to database.

I want to do next things:

  • save data in database (booth objects)
  • validate fields (I have validation in model)
  • and if validation fail, I want to populate fields with entered data
  • edit action for this form

Problems:

  • If I use @report I get:

Called id for nil, which would mistakenly be 4 error

(can't find object). I have in controller, in encreate action @report = ReportMain.new and in action that render that view.

  • When I use :report_main (model name) it works, it save data to database, but I can't get fields populated when validation fails.

Questions:

  • What to do with this two models to make this to work (validation, populating fields, edit)?
  • Can you give me some advice if approach is wrong?

My view looks like this:

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

<%= render "shared/error_messages", :target => @report %>

<%= f.text_field(:amount) %>


<% fields_for @reporte do |r| %>
    <%= r.check_box(:q_pripadnost) %>Pripadnost Q listi


    <%= select_tag('nacinpakovanja',options_for_select([['Drveno bure', 'Drveno bure'], ['Kanister', 'Kanister'], ['Sanduk', 'Sanduk'], ['Kese', 'Kese'], ['Posude pod pritiskom', 'Posude pod pritiskom'], ['Kompozitno pakovanje', 'Kompozitno pakovanje'], ['Rasuto', 'Rasuto'], ['Ostalo', 'Ostalo']])) %> 

    <%= r.text_field(:ispitivanjebroj) %>
    <%= r.text_field(:datumispitivanja) %>


<% end %>


<input id="datenow" name="datenow" size="30" type="text" value="<%= @date %>">

<div class="form-buttons">
       <%= submit_tag("Unesi izvestaj") %>
</div>

<% end %>

encreate actin in ReportController:

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 = @reportpar开发者_StackOverflowam[:amount]
    @report.isimport = false
    @report.isfinished = false
    @report.reportnumber =  ReportMain.where(:company_id => current_user.company_id, :isimport => false).count.to_i+1
    if @report.save
      @reporte.report_main_id = @report.id
    else
      redirect_to(:action => 'exportnew')
      return
    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)
    else
      redirect_to(:action => 'exportnew')
    end
  end


I think your problem in this case is that you use redirect_to instead of render. When you use redirect_to then you lose all the variables from your current action. I would probably do something like this in your encreate action:

if @reporte.save
  render :show
else
  render :exportnew
end

When you use render then it will use the variables from the current action but the view from the action you send to the render method. So when form_for is called with the @report variable, it is already populated with the values that was sent to encreate. Just make sure that you use the same variable names in the different actions but it looks like you do that already.

0

精彩评论

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

关注公众号