2016-10-12 22 views
0

データをモデルに保存できません。コードが実行されるたびに、CREATEアクションでデータを保存できなかったelseステートメントが実行されます。何か案が?モデルにデータを保存できません

これは私のinvoices_controller.rb

クラスInvoicesController < ApplicationControllerに

def new 
    @permits = Permit.find(params[:permit_id]) 
    @invoice = Invoice.new 
    end 

    def create 
    @permit = Permit.find(params[:permit_id]) 
    @invoice = @permit.build_invoice(invoice_params) 
    if @invoice.save 
     redirect_to payment_path 
    else 
     redirect_to root_path 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_invoice 
    @invoice = Invoice.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def invoice_params 
    params.require(:invoice).permit(:vehicle_type, :name, :department, :carplate, :duration, :permitstart, :permitend, :price, :time) 
    end 
end 

請求書/ new.html.erb(これは私が保存したいデータである)

<% provide(:title, 'Invoice') %> 
<h1>Invoice</h1> 

<div class="row"> 
    <div class="col-md-6 col-md-offset-3" id="datashow"> 

    <%= form_for(@invoice) do |f| %> 
    <h2>Time : <%[email protected]_at%></h2></br> 
    <h2>Invoice ID : <%[email protected]%></h2></br> 

     <%= f.label :"Vehicle" %> 
     <%= f.text_field :vehicle_type, :value => @permits.vehicle_type, readonly: true %> 

     <%= f.label :"License Plate" %> 
     <%= f.text_field :carplate, :value => @permits.carplate, readonly: true %> 

     <%= f.label :"Student ID" %> 
     <%= f.text_field :studentid, :value => @permits.studentid, readonly: true %> 

     <%= f.label :name %> 
     <%= f.text_field :name, :value => @permits.name, readonly: true %> 

     <%= f.label :"Department of applicant" %> 
     <%= f.text_field :department, :value => @permits.department, readonly: true %> 

     <%= f.label :permit_start %> 
     <%= f.text_field :permitstart, :value => @permits.permitstart, readonly: true %> 

     <%= f.label :permit_end %> 
     <%= f.text_field :permitend, :value => @permits.permitend, readonly: true %> 

    <%= f.label :"Price" %> 
    <%= (f.text_field :price, :value => '$AUD 50' , readonly: true) %> 

     <%= hidden_field_tag(:permit_id, @permits.id) %> 
     <%= f.submit "Make Payment", class: "btn btn-primary" %> 
    <% end %> 
    </div> 
</div> 

請求書であります.rb

class Invoice < ApplicationRecord 
    belongs_to :user 
    has_one :receipt 
    belongs_to :permit 
end 

Permit.rb

class Permit < ApplicationRecord 
    belongs_to :user 
    has_one :invoice 

end 
+0

火災アップ(PRY、byebug、等...)。すべての深刻なRailsの開発には、とにかく1つ必要です –

答えて

2

あなたのオブジェクトが作成されていない理由が不明な場合は、複数のオプションがあります。 最初にデバッグ中に@invoice.saveの代わりに@invoice.save!を使用できます。これにより例外が発生し、いくつかの手がかりが与えられます。間違ったことが起こります。

デバッガを使用して@invoice.errors.full_messagesを調べることができます。

さらに詳しく@invoice.errors.full_messagesRails.logger.error @invoice.errors.full_messages.to_sentenceで出力できます。

それとも、これはあなたがエラーを見つける必要がありますフラッシュメッセージflash[:error] = @item.errors.full_messages.to_sentence

ようなエラーメッセージを使用することができます。

0

から:ビルドは、ビューは、このオブジェクトを取得し、特にフォームのために、何かを表示することができるように、単にメモリ内の新しいオブジェクトを作成し、データベースのレコードを「作成」しませんbuild method on ruby on rails

レコードを作成(作成および保存)していないため、ビルドが機能していません。ビルドはレコードを保存しません。

試してみてください。デバッガ

def create 
    @permit = Permit.find(params[:permit_id]) 
    @invoice = @permit.invoices.create(invoice_params) 
    if @invoice.save 
    redirect_to payment_path 
    else 
    redirect_to root_path 
end 
end 
関連する問題