Ruby on rails 如何通过表单创建多态关联?
在我的Ruby on Rails应用程序中,我有一个多态关联,如下所示:Ruby on rails 如何通过表单创建多态关联?,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,在我的Ruby on Rails应用程序中,我有一个多态关联,如下所示: class Invoice < ApplicationRecord belongs_to :invoiceable, polymorphic: true end class Person < ApplicationRecord has_many :invoices, as: :invoiceable end class Company < ApplicationRecord has_man
class Invoice < ApplicationRecord
belongs_to :invoiceable, polymorphic: true
end
class Person < ApplicationRecord
has_many :invoices, as: :invoiceable
end
class Company < ApplicationRecord
has_many :invoices, as: :invoiceable
end
<%= f.select :invoiceable, current_user.people.options + current_user.companies.options %>
谢谢你的帮助。好的,我最后做了些事情。也很有帮助。好的,我最后做了一些事情。也很有帮助。您可以尝试在
@invoice=…
行之后放置一个绑定.pry
(如果没有,添加gempry
)。然后观察你的发票参数中有什么,并做你必须做的事情使其工作:)你可以尝试在行后面放置一个绑定。pry
(如果你没有,添加gempry
)。然后观察发票参数中的内容,并采取必要措施使其正常工作:)
def create
@invoice = current_user.invoices.build(invoice_params)
if @invoice.save
flash[:success] = "Invoice saved."
redirect_to invoice_path(@invoice)
else
render :new
end
end