gpt4 book ai didi

ruby-on-rails - 我如何在表单中包含 Rails 连接表字段?

转载 作者:行者123 更新时间:2023-12-04 20:17:31 26 4
gpt4 key购买 nike

在这种情况下,我可以将值插入到 Member 表和 Club 表中。但是有一个字段叫:taskmemberships我想向其提交值的表,以及成员(member)表 member_idclub_id由 Rails 自动插入。我如何包含 task以下表格中的字段?先感谢您。

View /表单:

<%= form_for @member ,:url=>{:action =>"create"} do |f| %>
<%= f.text_field :email %>
<%= f.fields_for :clubs do |s| %>
<%= s.text_field :name %>
<% end %>
<%= f.submit "submit" %>
<% end %>

楷模
class Member < ActiveRecord::Base
has_many :clubs ,:through=> :memberships
has_many :memberships
accepts_nested_attributes_for :clubs
attr_accessible :clubs_attributes
end

class Club < ActiveRecord::Base
has_many :members ,:through=>:memberships
has_many :memberships
end

class Memberships < ActiveRecord::Base
belongs_to :Member
belongs_to :Club
end

最佳答案

<%= f.fields_for :memberships do |m| %>
<%= m.text_field :task %>
<%= m.fields_for :clubs do |s| %>
<%= s.text_field :name %>
<% end %>
<% end %>

关于ruby-on-rails - 我如何在表单中包含 Rails 连接表字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10326551/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com