Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何通过表单创建多态关联?_Ruby On Rails_Ruby_Activerecord - Fatal编程技术网

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

在我的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_many :invoices, as: :invoiceable
end
<%= f.select :invoiceable, current_user.people.options + current_user.companies.options %>

谢谢你的帮助。

好的,我最后做了些事情。也很有帮助。

好的,我最后做了一些事情。也很有帮助。

您可以尝试在
@invoice=…
行之后放置一个
绑定.pry
(如果没有,添加gem
pry
)。然后观察你的发票参数中有什么,并做你必须做的事情使其工作:)你可以尝试在
行后面放置一个
绑定。pry
(如果你没有,添加gem
pry
)。然后观察发票参数中的内容,并采取必要措施使其正常工作:)
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