You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用form_with嵌套方式一次性保存项目及关联用户记录

实现创建项目时批量关联用户(has_many through + form_with)

1. 确认模型关联配置

首先确保三个模型的关联关系正确设置:

User 模型

class User < ApplicationRecord
  has_many :user_projects
  has_many :projects, through: :user_projects
end

Project 模型

class Project < ApplicationRecord
  has_many :user_projects
  has_many :users, through: :user_projects
end

UserProject 模型

class UserProject < ApplicationRecord
  belongs_to :user
  belongs_to :project
end

2. 配置控制器强参数与动作逻辑

在 ProjectsController 中,处理新建和创建动作,重点是允许 user_ids 数组参数:

class ProjectsController < ApplicationController
  def new
    @project = Project.new
    @users = User.all # 加载所有用户供选择
  end

  def create
    @project = Project.new(project_params)
    if @project.save
      redirect_to @project, notice: '项目创建成功,已完成用户关联'
    else
      @users = User.all # 验证失败时重新加载用户列表
      render :new
    end
  end

  private

  def project_params
    params.require(:project).permit(:name, user_ids: []) # 允许接收user_ids数组
  end
end

3. 编写form_with表单视图

在 app/views/projects/new.html.erb 中,使用 collection_check_boxes 辅助方法生成用户复选框,自动处理关联参数:

<%= form_with(model: @project, local: true) do |form| %>
  <% if @project.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@project.errors.count, "错误") %> 阻止项目保存:</h2>
      <ul>
        <% @project.errors.each do |error| %>
          <li><%= error.full_message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= form.label :name %>
    <%= form.text_field :name %>
  </div>

  <div class="field">
    <label>选择关联用户</label>
    <%= form.collection_check_boxes :user_ids, @users, :id, :email do |checkbox| %>
      <%= checkbox.check_box %>
      <%= checkbox.label %>
      <br>
    <% end %>
  </div>

  <div class="actions">
    <%= form.submit '创建项目' %>
  </div>
<% end %>

关键说明

  • collection_check_boxes 会自动生成格式为 project[user_ids][] 的复选框name属性,提交时选中的用户ID会以数组形式传递给控制器。
  • Rails的关联机制会自动处理 user_ids 参数,在保存项目的同时批量创建对应的 UserProject 关联记录,无需手动编写关联创建逻辑。
  • 若表单验证失败,页面会保留之前填写的项目信息和勾选的用户状态,提升用户体验。

内容的提问来源于stack exchange,提问作者Muhammad Ans

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 22:25:18