Rails 3.2
在lib / model_extension.rb中,我有:
module ModelExtension
def generate_model_id
self.id = "#{Time.now.to_f.to_s.gsub /\./, '_'}_#{self.class.name.underscore}" if id.blank?
end
def add_ticket_id_to_model(ticket_id)
self.ticket_id = ticket_id
end
end
在controllers / admin / lead_billings_controller.rb中,我有:
def create
@lead_billing = LeadBilling.new(params[:lead_billing])
@lead_billing.generate_model_id
@lead_billing.add_ticket_id_to_model(ticket_id)
@ticket = Ticket.find(params[:ticket_id])
respond_to do |format|
if @lead_billing.save
format.html { redirect_to @ticket, notice: 'Lead billing was successfully created.' }
format.json { render json: @lead_billing, status: :created, location: @lead_billing }
else
format.html { render action: "new" }
format.json { render json: @lead_billing.errors, status: :unprocessable_entity }
end
end
end
在models / lead_billing.rb中,这里有我所拥有的:
class LeadBilling < ActiveRecord::Base
include ModelExtension
attr_accessible :ticket_id, :pre_tax_total, :post_tax_total
belongs_to :ticket
end
在views / lead_billings / leadbillings.html.slim中,我有:
- pre_tax_total = @ticket.lead_billing.pre_tax_total.to_f
- post_tax_total = @ticket.lead_billing.post_tax_total.to_f
table.table.ajax-table.show-mode
tr.head-row
th Pre-Tax Total
th Post-Tax Total
tr
td = number_to_currency(pre_tax_total, :unit => "$")
td = number_to_currency(post_tax_total, :unit => "$")
- if lead_billing.status == 'entered'
= form_for(LeadBilling.new, url: lead_billing_path) do |f|
= hidden_field_tag :ticket_update_type, "complete_and_generate_lead_invoices"
= hidden_field_tag :ticket_id, @ticket.id
.form-horizontal-column.customer-info
.form-group
.form-horizontal-column.customer-info
.actions = f.submit 'Complete And Generate Invoices'
.clear
当我填写并提交表单时,我收到一条错误消息,这就是我在日志文件中的内容:
Started POST "/admin/lead_billings" for 162.17.182.1 at 2016-12-01 18:36:01 +0000
Processing by Admin::LeadBillingsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xxx", "ticket_update_type"=>"save_lead_billing", "ticket_id"=>"1473264666_2463856_ticket", "lead_billing"=>{"pre_tax_total"=>"100", "post_tax_total"=>"120"}, "commit"=>"Save Lead Billing Details"}
.......................
NameError (undefined local variable or method `ticket_id' for #<Admin::LeadBillingsController:0x0000000919e368>):
app/controllers/admin/lead_billings_controller.rb:44:in `create'
app/middleware/catch_json_parse_errors.rb:8:in `call'
我仔细检查了lead_billings表,它有ticket_id列。
有什么想法吗?
答案 0 :(得分:1)
在您的控制器@lead_billing.add_ticket_id_to_model(ticket_id)
中,您错过了设置ticket_id
或仅在该行上说params[:ticket_id]