【发布时间】:2013-01-28 19:25:16
【问题描述】:
好的,我搜索了高低,阅读教程,观看了视频,但我仍然没有得到任何结果。我在这里读过类似的问题,但问题更复杂或缺乏答案 - 所以这里......
我有模型帐户和发票。显示帐户时,我想要与该帐户相关的“创建新发票”链接。 (稍后我实际上希望在创建发票时选择一个选择字段来选择一个帐户,但我将把它留给另一个痛苦)。
这是我的模型... 帐号:
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>,然后当我提交表单时出现此错误:
InvoicesController#create 中的 ActiveRecord::AssociationTypeMismatch
用这个声明: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
以上是我认为我出错的地方,但目前这些行的内容是我无法理解的。我完全是一个初学者,任何解决此功能的帮助肯定会教给我很多东西。
感谢您的宝贵时间。
【问题讨论】:
-
您的错误出现在您的发票控制器中,因此最好在您的问题中显示相应的代码以及相关表格
-
@siekfried 谢谢,我已经更新以显示控制器的代码,表单只是脚手架为新发票生成的通用格式。这有帮助吗?
-
@siekfried 给了我一个 NameError...你的意思是
[invoice]as[:invoice]吗?我也试过了,没有运气..
标签: ruby-on-rails-3 rails-models