2014-11-23 110 views
0

我正在努力與此,似乎應該很簡單 - 我只是不明白爲什麼它不會工作。link_to與循環內的嵌套路由

我在我的付款視圖中有以下循環,我想link_to刪除和編輯。 我傳入@bill對象和past_payment對象。

我得到這個錯誤:沒有路由匹配{:action =>「edit」,::bill_id => 11,:controller =>「payments」,:id => nil}缺少必需的鍵:[:id ]

我看不出爲什麼我不能從past_payment得到一個ID:我試過使用past_payment.id,並通過將它作爲表的一部分進行打印來確保它存在於對象上。似乎工作,如果我靜靜地輸入一個ID。

<div class="row"> 
    <div class="col-md-6"> 
    <h4>Payment for : <%= @bill.vendor.full_vendor %></h4> 
    <p>Amount Owing: <%= humanized_money_with_symbol @bill.owing %></p> 
    <p>Due: <%= @bill.due_date.to_formatted_s(:rfc822) %> </p> 
    </div> 

    <div class="col-md-6"> 
    <h4>Payment Options</h4> 
    <table class="table table-striped table-responsive"> 
     <tr> 
     <td><%=image_tag("40px_BPAY_2012_PORT_BLUE.png", alt: "BPay_Image")%></td> 
     <td><p>biller code:</br><%= @bill.vendor.bpay_biller_code %></p></td> 
     <td><p>ref number:</br><%= @bill.vendor.bpay_ref %></p></td> 
     </tr> 
    </table> 
    </div> 

</div> 

<div class="row col-md-12"> 
    <h4>This Payment</h4> 
</div> 

    <%= form_for(@payment, url: :bill_payments, html: { method: "post", class: "form-horizontal", role: "form" }) do |payment| %> 

    <div class="form-group col-md-12"> 
     <%= payment.label :amount, class: "control-label sr-only" %> 
     <%= payment.text_field :amount, data: { role: 'money', aSign: '$'}, class: "form-control", placeholder: "Amount Paid" %> 

     <%= payment.label :receipt_number, class: "control-label sr-only" %> 
     <%= payment.text_field :receipt_number, class: "form-control", placeholder: "Receipt #" %> 

     <%= payment.label :notes, class: "control-label sr-only" %> 
     <%= payment.text_area :notes, class: "form-control", rows: "1", placeholder: "Notes" %> 

     <div class="form-inline"> 
     <%= payment.label :payment_date, class: "control-label sr-only" %> 
     <%= payment.date_select :payment_date, { order: [ :day, :month, :year ], 
     start_year: 2014, 
     end_year:2020 }, 
     class: "form-control" %> 
     </div> 
    </div> 

    <%= payment.submit "Pay", class: "btn btn-default" %> 
    <% end %> 


</br> 
<div class="row"> 
    <div class="col-md-12"> 
    <h4>Previous Payments</h4> 
     <table class="table table-striped table-responsive"> 
     <tr> 
      <th>Amount Paid</th> 
      <th>Receipt #</th> 
      <th>Date Paid</th> 
      <th colspan="2">Actions</th> 
     </tr> 
     <% @bill.payments.each do |past_payment| %> 
     <tr> 
      <td><p><%= humanized_money_with_symbol past_payment.amount %></p></td> 
      <td><p><%= past_payment.receipt_number %></p></td> 
      <td> 
      <% if not past_payment.payment_date == nil %> 
       <p><%= past_payment.payment_date.to_formatted_s(:rfc822) %></p> 
      <% end %> 
      </td> 
-->  <td> 
       <%= link_to "Edit", edit_bill_payment_path(@bill, past_payment), method: :patch %> 
      </td> 
      <td> 
       <%= link_to "Delete", bill_payment_path(@bill, past_payment), method: :delete, 
-->    data: { confirm: "Are you sure?" } %> 
      </td> 
     </tr> 
     <% end %> 
     </table> 
    </div> 
</div> 

控制器代碼

class PaymentsController < ApplicationController 
    before_action :authenticate_user! 


    def new 
    @bill=Bill.find(params[:bill_id]) 
    @[email protected] 
    end 

    def edit 
    @bill=Bill.find(params[:bill_id]) 
    @[email protected](:id) 
    end 

    def update 
    @bill=Bill.find(params[:bill_id]) 
    if @bill.payments(payment_params).update 
     flash[:success] = "This payment has been updated" 
     redirect_to new_bill_payment_url(params[:bill_id]) 
    else 
     flash[:error] = "There has been a problem updateing this payment" 
     redirect_to :back 
    end 
    end 

    def create 
     @bill=Bill.find(params[:bill_id]) 
    if @bill.payments.save 
     flash[:success] = "This payment has been registered" 
     redirect_to new_bill_payment_url(params[:bill_id]) 
    else 
     flash[:error] = "There has been a problem with this payment" 
     redirect_to :back 
    end 
    end 

    def destroy 
    @bill=Bill.find(params[:bill_id]) 

    if @bill.payments.find(params[:id]).destroy 
     flash[:success] = "This payment has been removed" 
     redirect_to new_bill_payment_url(params[:bill_id]) 
    else 
     flash[:error] = "There has been a problem removing this payment" 
     redirect_to :back 
    end 
    end 


private 

    def payment_params 
    params.require(:payment).permit(:id, :bill_id,:receipt_number, :notes, :amount, 
               :payment_date) 
    end 

end 

路線片段

resources :bills do 
    resources :payments, only: [:new, :create, :edit, :destroy] 
end 

比爾型號

class Bill < ActiveRecord::Base 

    belongs_to :user 
    belongs_to :vendor 
    has_many :payments, dependent: :destroy 
    accepts_nested_attributes_for :payments, allow_destroy: true 

    validates :vendor_id, :amount, :due_date, :bill_type, presence: true 
    validate :over_payment 

    monetize :amount_cents 
    monetize :gst_cents 
    monetize :owing_cents 

    def owing_cents 
    self.amount_cents - self.payments.sum(:amount_cents) 
    end 

    def gst_cents 
    self.amount_cents/11 
    end 

private 
    def over_payment 
    if self.payments.sum(:amount_cents) > owing_cents 
     errors.add(:amount, "Over payment of a bill not allowed 
     add interest or penitalties as a negitive payment first") 
    end 
    end 
end 

感謝您的幫助。

+0

你可以給你的路線片段嗎? – Rubyrider 2014-11-23 04:33:47

+0

請分享@bill正在初始化的控制器代碼。 – Anand 2014-11-23 04:33:53

+0

這是從'new'動作引發的錯誤嗎? – blelump 2014-11-23 07:19:00

回答

0

以下解決我的問題。

<td> 
    <%= link_to "Edit", edit_bill_payment_path(@bill, id: "#{past_payment.id}"), method: :patch %> 
</td> 
<td> 
    <%= link_to "Delete", bill_payment_path(@bill, id: "#{past_payment.id}"), method: :delete, data: { confirm: "Are you sure?" } %> 
</td> 

我不確定這是最佳實踐還是爲什麼使用past_payment對象不起作用。我認爲需要做更多的研究。

感謝那些回覆。

1

在您使用的行爲中,但是@bill:id,而不是:bill_id。這就是問題。你真正想要的是使用@bill=Bill.find(params[:id])

您使用的鏈接:<%= link_to "Pay", new_bill_payment_path(bill) %>給你:id=>nil,因爲沒有票據的對象,但@bill,因此鏈接最有可能應該是<%= link_to "Pay", new_bill_payment_path(@bill) %>

+0

謝謝Andrey - The new action in the支付控制器被調用:'<%= link_to「支付」,new_bill_payment_path(bill)%>'這是路由通過bill_id – Smithy 2014-11-23 09:34:08

+0

它必須是'<%= link_to「支付」,new_bill_payment_path(@bill)%>'然後 – 2014-11-23 09:40:00

+0

Andrey謝謝,我給了它一個去,它並沒有解決這個問題,我可能從根本上失去了一些東西,但支付控制器嵌套在賬單下,所以路線'/bills/:bill_id/payments/new(.:format)'正在發送bill_id。如果我刪除鏈接,付款新操作會提取bill_id並生成@bill ed_to位一切正常。我已經添加了賬單模型,它可能會顯示更多細節。 – Smithy 2014-11-24 11:44:13