无法将数据保存到模型中

时间:2016-10-12 17:54:50

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4

我无法将数据保存到我的模型中。每次代码运行时,它都会遇到else语句,该语句无法在CREATE操作中保存数据。有什么想法吗?

这是我的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(这是我想保存的数据)

<% 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

2 个答案:

答案 0 :(得分:2)

如果您不确定为什么不创建对象,则可以有多个选项。 首先,您可以在调试期间使用@invoice.save!代替@invoice.save。这将引发一个例外并给你一些线索,出了什么问题。

或者您可以使用调试器并检查@invoice.errors.full_messages

您还可以通过@invoice.errors.full_messages输出Rails.logger.error @invoice.errors.full_messages.to_sentence

或者您可以将错误消息用作Flash消息flash[:error] = @item.errors.full_messages.to_sentence

这可以帮助您找到错误。

答案 1 :(得分:0)

来自:build method on ruby on rails

建立赢得&#34;创造&#34;数据库中的记录,只需在内存中创建一个新对象,以便视图可以获取此对象并显示某些内容,尤其是对于表单。

因此,您无法创建(创建和保存)记录,因此构建不起作用。 build并不保存记录。

尝试:

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