我有一个简单的表格,记录收到的付款:
<label>Client Name*</label>
<%= f.collection_select :client_id, Client.all, :id, :name, {prompt:"Select Client"},{ class: "form-control"} %>
<label>Service Name</label>
<%= f.collection_select :service_id, Service.all, :id, :name, {prompt: "Select Service"},{ class: "form-control"}%><label>Payment Amount</label>
<%= f.text_field :amount, placeholder: "Enter the amount received",class: "form-control" %>
<label>Payment Date</label> <br/>
<%= f.date_select :recieved_date, placeholder: "Enter the payment date",class: "form-control" %><br/>
<br/>
<%= f.submit "Submit", class: "btn btn-success" %>
<% end %>
然后在控制器上传递值:
def create
@payment = Payment.new(payment_params)
order = Order.where("client_id = ? and service_id = ?", payment_params[:client_id], payment_params[:service_id]).pluck(:id)
@payment.order_id = order[0]
if @payment.save
redirect_to new_payment_path, notice: "Successfully created Payment!"
else
render action: 'new'
end
end
正如你可以观察到的那样 order = Order.where(“client_id =?and service_id =?”,payment_params [:client_id],payment_params [:service_id])。pluck(:id)尝试根据client_id和service_id存储order_id。但是这不起作用,订单对象在保存时以零值保存(当检查order_id的验证被删除时)
这是为什么?任何提示,请