好的,我已经寻找了许多资料、观看了视频,但在这方面仍然一无所获。我在这里阅读了类似的问题,但是这些问题更加复杂或者缺乏答案——所以接下来就是我的问题……
我有两个模型:Account(账户)和Invoice(发票)。当显示一个账户时,我想要一个链接来“创建新发票”,并将其关联到该账户。(稍后我实际上希望在创建发票时选择一个账户,但我将把这留给另一个问题。)
以下是我的模型:
Account:class Account < ActiveRecord::Base
attr_accessible :name, :invoice
attr_accessible :name, :invoice
has_many :invoices
end
发票:
class Invoice < ActiveRecord::Base
belongs_to :account
attr_accessible :amount_pretax, :amount_total, :date_sent, :project, :status, :tax, :account, :account_id
end
现在,在我的/views/accounts/show.html.erb中。
<p id="notice"><%= notice %></p>
<p>
<b>Name:</b>
<%= @account.name %>
</p>
<%= link_to 'New Invoice', new_invoice_path(:account_id=>@account.id) %>
<%= link_to 'Edit', edit_account_path(@account) %> |
<%= link_to 'Back', accounts_path %>
所以,现在的情况是,当我点击“新发票”链接时,它会显示新的表单,其中账户字段填充了这个奇怪的文本:#<Account:0x10fe16bc0>
,然后当我提交表单时,我会得到这个错误:
ActiveRecord::AssociationTypeMismatch in InvoicesController#create
伴随着这个语句:Account(#2281084000) expected, got String(#2267210740)
还有这个:
app/controllers/invoices_controller.rb:45:in `new'
app/controllers/invoices_controller.rb:45:in `create'
以下是发票控制器中的内容:
def new
@invoice = Invoice.new(:account_id => params[:account_id])
respond_to do |format|
format.html # new.html.erb
format.json { render :json => @invoice }
end
end
def create
@invoice = Invoice.new(params[:invoice])
....
end
以下是我认为出错的地方,但是目前我不知道在这些行中应该放什么。我完全是一个初学者,在解决这个功能方面提供任何帮助肯定会教给我很多。
感谢您的时间。
[invoice]
而不是[:invoice]
? 我也尝试了那个但没成功。 - Mark