Rich Join Table 的嵌套属性,使用 simple_form Rails
Nested Attributes for a Rich Join Table, using simple_form Rails
我想创建一个具有嵌套属性的表单,该表单在富连接中填充记录 table。 (在 rich join table 中创建的记录当然应该有适当的外键。)
我还没有找到关于在 has_many :through
关系上创建嵌套表单字段的完整答案。请帮忙!
对于这个例子,我有一个用户表单。在该表格中,我还尝试在 users_pets
table(rich join table)中填充一条记录。
附加问题:rich join 模型应该是单数还是复数?示例:app/models/owners_pets.rb
或 app/models/owners_pet.rb
。
app/models/owner.rb
class Owner < ActiveRecord::Base
has_many :owners_pets, allow_destroy: true
has_many :pets, through: :owners_pets
end
app/models/pet.rb
class Pet < ActiveRecord::Base
has_many :owners_pets, allow_destroy: true
has_many :owners, through: :owners_pets
end
app/models/owners_pets.rb
class OwnersPet < ActiveRecord::Base
belongs_to :owners
belongs_to :pets
end
app/controller/owners.rb
def owner_params
params.require(:owner).permit(:first_name, owners_pets_attributes: [:id, :pet_name, :pet_id])
end
app/views/owners/_form.html.erb
<%= simple_form_for(@owner) do |f| %>
<%= f.input :first_name %>
<%= f.simple_fields_for :owners_pets do |ff|
<%= ff.input :pet_name %>
<% end %>
<div>
<%= f.button :submit %>
</div>
<% end %>
你加入table的问题是:
加入 table 应该 belongs_to :owners belongs_to :pets
才能工作
此外,rich join 模型应该是多元化的,如:owners_pets
答案在这里,感谢导师的大量帮助。它帮助我记住富连接命名约定不应该在最后被复数化,就像其他非富连接模型一样。例如:book_page.rb
NOT books_pages.rb.
即使 books_page.rb
也可以工作(只需相应地更新您的强参数和数据库 table)。重要的是整个模型必须遵循rails模型的约定(最后没有's')。
在下面的富连接模型中,我决定将其命名为 完全单数版本:owner_pet.rb
而不是另一个版本:owners_pet.rb
. (因此,当然,我的数据库 table 被命名为:owner_pets
)
app/models/owner.rb
class Owner < ActiveRecord::Base
has_many :owner_pets
has_many :pets, through: :owner_pets
accepts_nested_attributes_for :owner_pets, allow_destroy: true
end
app/models/pet.rb
class Pet < ActiveRecord::Base
has_many :owner_pets
has_many :owners, through: :owner_pets
end
app/models/owner_pet.rb
class OwnerPet < ActiveRecord::Base
belongs_to :owner
belongs_to :pet
end
app/controller/owners.rb
def new
@owner = Owner.new
@owner.owner_pets.build
end
private
def owner_params
params.require(:owner).permit(:first_name, owner_pets_attributes: [:_destroy, :id, :pet_name, :pet_id, :owner_id])
end
app/views/owners/_form.html.erb
<%= simple_form_for(@owner) do |f| %>
<%= f.input :first_name %>
<%= f.simple_fields_for :owner_pets do |ff| %>
<%= ff.input :pet_name %>
<%= ff.input :pet_id, collection: Pet.all, label_method: "pet_type" %>
<% end %>
<div>
<%= f.button :submit %>
</div>
<% end %>
我想创建一个具有嵌套属性的表单,该表单在富连接中填充记录 table。 (在 rich join table 中创建的记录当然应该有适当的外键。)
我还没有找到关于在 has_many :through
关系上创建嵌套表单字段的完整答案。请帮忙!
对于这个例子,我有一个用户表单。在该表格中,我还尝试在 users_pets
table(rich join table)中填充一条记录。
附加问题:rich join 模型应该是单数还是复数?示例:app/models/owners_pets.rb
或 app/models/owners_pet.rb
。
app/models/owner.rb
class Owner < ActiveRecord::Base
has_many :owners_pets, allow_destroy: true
has_many :pets, through: :owners_pets
end
app/models/pet.rb
class Pet < ActiveRecord::Base
has_many :owners_pets, allow_destroy: true
has_many :owners, through: :owners_pets
end
app/models/owners_pets.rb
class OwnersPet < ActiveRecord::Base
belongs_to :owners
belongs_to :pets
end
app/controller/owners.rb
def owner_params
params.require(:owner).permit(:first_name, owners_pets_attributes: [:id, :pet_name, :pet_id])
end
app/views/owners/_form.html.erb
<%= simple_form_for(@owner) do |f| %>
<%= f.input :first_name %>
<%= f.simple_fields_for :owners_pets do |ff|
<%= ff.input :pet_name %>
<% end %>
<div>
<%= f.button :submit %>
</div>
<% end %>
你加入table的问题是:
加入 table 应该 belongs_to :owners belongs_to :pets
才能工作
此外,rich join 模型应该是多元化的,如:owners_pets
答案在这里,感谢导师的大量帮助。它帮助我记住富连接命名约定不应该在最后被复数化,就像其他非富连接模型一样。例如:book_page.rb
NOT books_pages.rb.
即使 books_page.rb
也可以工作(只需相应地更新您的强参数和数据库 table)。重要的是整个模型必须遵循rails模型的约定(最后没有's')。
在下面的富连接模型中,我决定将其命名为 完全单数版本:owner_pet.rb
而不是另一个版本:owners_pet.rb
. (因此,当然,我的数据库 table 被命名为:owner_pets
)
app/models/owner.rb
class Owner < ActiveRecord::Base
has_many :owner_pets
has_many :pets, through: :owner_pets
accepts_nested_attributes_for :owner_pets, allow_destroy: true
end
app/models/pet.rb
class Pet < ActiveRecord::Base
has_many :owner_pets
has_many :owners, through: :owner_pets
end
app/models/owner_pet.rb
class OwnerPet < ActiveRecord::Base
belongs_to :owner
belongs_to :pet
end
app/controller/owners.rb
def new
@owner = Owner.new
@owner.owner_pets.build
end
private
def owner_params
params.require(:owner).permit(:first_name, owner_pets_attributes: [:_destroy, :id, :pet_name, :pet_id, :owner_id])
end
app/views/owners/_form.html.erb
<%= simple_form_for(@owner) do |f| %>
<%= f.input :first_name %>
<%= f.simple_fields_for :owner_pets do |ff| %>
<%= ff.input :pet_name %>
<%= ff.input :pet_id, collection: Pet.all, label_method: "pet_type" %>
<% end %>
<div>
<%= f.button :submit %>
</div>
<% end %>