尝试在另一个控制器显示页面中调用其他控制器的窗体时出现参数错误

时间:2015-04-02 07:50:05

标签: ruby-on-rails

我有两个型号,其中invoice_details has_many multiple_goods和multiple_goods belongs_to invoice_details。

我有一个条件,当我点击新的时候,我必须在invoice_details_show中显示multiple_goods的形式作为弹出窗口。

invoice_details / show.html.erb:

   <div><%= link_to 'New Person', '#new_person_modal', 'data-toggle' => 'modal' %></div>

    <%# Bootstrap modal markup. @see: http://getbootstrap.com/javascript/#modals %>
    <div class="modal fade" id="new_person_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4 class="modal-title" id="myModalLabel">Create new person</h4>
          </div>
          <div class="modal-body">
            <%# Render the new person form (passing modal => true to enable remote => true) %>
            <%= render 'multiple_goods/form', modal: true %>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          </div>
        </div>
      </div>
    </div>

multiple_goods / _form.html.erb:

<%=form_for([:invoice_detail,@multiple_good], html: {class: 'form-horizontal', role: 'form' }) do |f| %>
  <% if @multiple_good.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@multiple_good.errors.count, "error") %> prohibited this multiple_good from being saved:</h2>

      <ul>
      <% @multiple_good.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
<br/>

    <div class="field">
    <%= f.label :description_of_goods1, :class => 'control-label' %>
                <div class="controls">

    <%= f.text_field :description_of_goods1, :class => 'text_field', :required => true,:maxlength => 20, :placeholder => '20 Alpha numeric characters'  %>
    </div>
    </div>
    <div class="field">
    <%= f.label :quatity1, :class => 'control-label' %>
                <div class="controls">

    <%= f.text_field :quatity1, :class => 'text_field', :required => true,:maxlength => 20, :placeholder => 'Enter quatity'  %>
    </div>
    </div>

    <div class="field">
    <%= f.label :price_per_unit1, :class => 'control-label' %>
                <div class="controls">

    <%= f.text_field :price_per_unit1, :class => 'text_field', :placeholder => 'Enter price Per unit'  %>
    </div>
    </div>
    <div class="field">
    <%= f.label :total_amount1, :class => 'control-label' %>
                <div class="controls">

    <%= f.text_field :total_amount1, :class => 'text_field', readonly: true, :placeholder => 'This field is auto saved'  %>
    </div>
    </div>


 <div class="form-actions2"style="text-align:center">
    <%= f.submit  :class => 'btn btn-primary' %>
  </div>
  </div>
<% end %>

最后我的控制器:

class MultipleGoodsController < ApplicationController
  before_action :set_multiple_good, only: [:show, :edit, :update, :destroy]

  respond_to :html

  def index
    @multiple_goods = MultipleGood.all
    respond_with(@multiple_goods)
  end

  def show
    respond_with(@multiple_good)
  end

  def new
    @multiple_good = MultipleGood.new
    respond_with(@multiple_good)
  end

  def edit
  end

  def create
    @invoice_detail = InvoiceDetail.find(params[:invoice_detail_id])
    @multiple_good = @invoice_detail.multiple_goods.create(multiple_good_params)
    redirect_to invoice_detail_path(@invoice_detail)
  end


  def update
    @multiple_good.update(multiple_good_params)
    respond_with(@multiple_good)
  end

  def destroy
    @multiple_good.destroy
    respond_with(@multiple_good)
  end

  private
    def set_multiple_good
      @multiple_good = MultipleGood.find(params[:id])
    end

    def multiple_good_params
      params.require(:multiple_good).permit(:description_of_goods1, :quatity1, :price_per_unit1, :total_amount1)
    end
end

我的发票详情控制器

class InvoiceDetailsController < ApplicationController
  before_action :set_invoice_detail, only: [:show, :edit, :update, :destroy]

  respond_to :html

  def index
    @invoice_details = InvoiceDetail.all
    respond_with(@invoice_details)
  end

  def show
 @invoice_detail = InvoiceDetail.find(params[:id])

  respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :xml => @invoice_detail }
    format.pdf { render :layout => false }
  end
  end

  def new
    @invoice_detail = InvoiceDetail.new
     @invoice_detail_attachment = @invoice_detail.invoice_detail_attachments.build

  end

  def edit
  end

  def create
    @invoice_detail = InvoiceDetail.new(invoice_detail_params)
    respond_to do |format|
      if @invoice_detail.save
        params[:invoice_detail_attachments]['avatar'].each do |a|
          @invoice_detail_attachment = @invoice_detail.invoice_detail_attachments.create!(:avatar => a, :invoice_detail_id => @invoice_detail.id)
       end
       format.html { redirect_to @invoice_detail, notice: '' }
     else
       format.html { render action: 'new' }
     end
   end
 end

  def update
    @invoice_detail.update(invoice_detail_params)
    if @invoice_detail.save
        params[:invoice_detail_attachments]['avatar'].each do |a|
          @invoice_detail_attachment = @invoice_detail.invoice_detail_attachments.create!(:avatar => a, :invoice_detail_id => @invoice_detail.id)
       end
    redirect_to invoice_details_path
  end
end


  def destroy
    @invoice_detail.destroy
  redirect_to invoice_details_path
  end
  def download
  require 'zip/zip'
  require 'zip/zipfilesystem'
  @invoice_detail = InvoiceDetail.find(params[:invoice_id])

  t = Tempfile.new('tmp-zip-' + request.remote_ip)
  Zip::ZipOutputStream.open(t.path) do |zos|
    @invoice_detail.invoice_detail_attachments.each do |file|
      zos.put_next_entry(file.invoice_detail)
      zos.print IO.read(file.avatar.path)     
    end
  end

  send_file t.path, :type => "application/zip", :filename => "#{@invoice_detail.invoice_number}.zip"

  t.close
end



  private
    def set_invoice_detail
      @invoice_detail = InvoiceDetail.find(params[:id])
    end

    def invoice_detail_params
      params.require(:invoice_detail).permit(:avatar,:attachment,:invoice_number, :supplier_name, :invoice_date, :invoice_due_date, :description_of_goods, :quatity, :price_per_unit, :total_amount, :mode_of_payment, :status, :shipping_country, :sl_no, :containers, :net_weight, :bl_number, :bl_date, :insurance_provider, :insurance_amount, :insurance_status, :coo_payment, :farworder, :farworder_inv_amt_doller, :farworder_inv_amt_idr, :payment_status_to_farworder,:final_amount )
    end
end

当我在服务器上运行时,我收到错误:

InvoiceDetails中的ArgumentError#show

表单中的第一个参数不能包含nil或为空

1 个答案:

答案 0 :(得分:0)

你在InvoiceDetailsController中显示的动作并没有为_form包含一个新的multiple_goods,你应该考虑到视图中的渲染只会使一个视图不能通过另一个动作。

def show
 @invoice_detail = InvoiceDetail.find(params[:id])
 @multiple_good = MultipleGood.new
  respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :xml => @invoice_detail }
    format.pdf { render :layout => false }
  end
  end