Php 到 Rails - Rails 关联 - contact_to_groups 表
我有 CRUD 来创建联系人和创建组。两者都嵌套在用户模型下。
我现在需要知道如何将联系人与群组关联起来。
我希望在我的联系表单中包含一些复选框(使用 formattastic),以便用户可以选择联系人所属的组。
在 php 中,我将创建一个名为 contact_to_groups 的表,并且我将有 contact_id & group_id 列,然后当我保存联系人时,我将传递该数据并使用联接稍后将其返回。
谢谢!
联系创建表单
<%= semantic_form_for [@contact.user, @contact] do |f| %>
<% f.inputs do %>
<%= f.input :firstname, :label => 'First Name' %>
<%= f.input :lastname, :label => 'Last Name' %>
<%= f.input :email, :label => 'Email' %>
<%= f.input :notes, :input_html => { :class => 'autogrow', :rows => 10, :cols => 50, :maxlength => 10 }, :label => 'Notes' %>
<% end %>
<%= f.buttons %>
<% end %>
I have CRUD in place for creating contacts and creating groups. Both are nested under the user model.
I need to know how I can now associate contacts with groups.
I would like in my contact form to have some checkboxes (using formtastic) so the user can select which group(s) the contact belongs to.
In php i would make a table called contacts_to_groups and i would have contact_id & group_id columns, then when I would save the contact i would pass that data and use a join to get it back out later.
Thanks!
contact create form
<%= semantic_form_for [@contact.user, @contact] do |f| %>
<% f.inputs do %>
<%= f.input :firstname, :label => 'First Name' %>
<%= f.input :lastname, :label => 'Last Name' %>
<%= f.input :email, :label => 'Email' %>
<%= f.input :notes, :input_html => { :class => 'autogrow', :rows => 10, :cols => 50, :maxlength => 10 }, :label => 'Notes' %>
<% end %>
<%= f.buttons %>
<% end %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样更正您的模型:
然后您需要在数据库 contacts_groups(contact_id, group_id) 中创建表
Correct your models like this:
And then you need to create table in DB contacts_groups(contact_id, group_id)