0

I can't save the data into my model. Every time when the code run it will ran into the else statement which failed to save the data in the CREATE action. Any idea?

This is my invoices_controller.rb

class 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

Invoices/new.html.erb ( This is the data I wanted to save)

<% 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 : <%=@permits.created_at%></h2></br>
    <h2>Invoice ID : <%=@permits.id%></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>

Invoice.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
Brad Werth
  • 17,411
  • 10
  • 63
  • 88
Gary Vlc
  • 141
  • 1
  • 2
  • 10

2 Answers2

2

If you are unsure why your object is not created, you have multiple options. First you can use @invoice.save! instead of @invoice.save during debugging. This will raise an exception and give you some clues, what's going wrong.

Or you can use a debugger and inspect @invoice.errors.full_messages.

Further more you can output @invoice.errors.full_messages via Rails.logger.error @invoice.errors.full_messages.to_sentence.

Or you can use the error message as a flash message flash[:error] = @item.errors.full_messages.to_sentence

This should help you find the error.

slowjack2k
  • 2,566
  • 1
  • 15
  • 23
0

from: build method on ruby on rails

build won't "create" a record in database, just create a new object in memory so that the view can take this object and display something, especially for a form.

So build isn't working because you aren't creating (create and saving) a record. build doesn't save a record.

Try:

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
Community
  • 1
  • 1
user3456978
  • 238
  • 1
  • 13