Ruby on Rails 将 id 传递给新的创建表单 [英] Ruby on Rails pass id to new create form
问题描述
好的,我已经搜索了高低,阅读了教程,观看了视频,但我仍然没有找到任何地方.我在这里读过类似的问题,但问题更复杂或缺乏答案 - 所以这里......
Ok, I've searched high and low, read tutorials, watched videos and I am still not getting any where with this. I've read similar questions here, but questions were more complex or lacked answers - so here goes...
我有帐户和发票模型.在显示帐户时,我想要一个指向与该帐户相关的创建新发票"的链接.(后来我实际上希望在创建发票时有一个选择字段来选择一个帐户,但我会把它留给另一个痛苦.
I have models Account and Invoice. When showing an Account, I'd like a link to 'Create new invoice' which relates to that account. (Later I'd actually like a select field to choose an Account when creating an Invoice, but I'll leave that to another excruciation).
这是我的模型...帐户:
Here are my models... 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
Now, in my /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::InvoicesController#create 中的AssociationTypeMismatch用这个语句:Account(#2281084000) expected, got String(#2267210740)
伴随着这个:
So, what's happening is, when I click on the New Invoice link it shows the new form, with the account field populated with this weird text: #<Account:0x10fe16bc0>
and then when I submit the form I get this error:
ActiveRecord::AssociationTypeMismatch in InvoicesController#create
with this statement: Account(#2281084000) expected, got String(#2267210740)
along with this:
app/controllers/invoices_controller.rb:45:in `new'
app/controllers/invoices_controller.rb:45:in `create'
这是发票控制器中的内容:
This is what is in the Invoices Controller:
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
以上是我认为我出错的地方,但目前我无法将这些台词放在哪里.我完全是一个初学者,解决此功能的任何帮助肯定会教我负担.
The above is where I think I'm going wrong, but what to put this those lines is beyond me at the moment. I'm totally a beginner, any help to solve this functionality will surely teach me loads.
感谢您的时间.
推荐答案
当您单击 /views/accounts/show
页面上的 New invoice
链接时,我想您希望您的新发票属于该帐户.
When you click the New invoice
link on the /views/accounts/show
page, I suppose that you want that your new invoice belongs to this account.
因此在您的表单中,您不必让用户选择帐户.例如,您可以用 hidden_field
替换相应的字段:
So in your form, you don't have to let the user choose an account. You can for example replace the corresponding field by a hidden_field
:
<%= f.hidden_field :account_id, :value => params[:account_id] %>
同样在控制器的 new
操作中,将 @invoice = Invoice.new(:account_id => params[:account_id])
替换为 @发票 = Invoice.new
Also in the new
action of your controller, replace @invoice = Invoice.new(:account_id => params[:account_id])
by @invoice = Invoice.new
希望这会有所帮助.
这篇关于Ruby on Rails 将 id 传递给新的创建表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!