Cocoon Gem:编辑视图仅显示空表单字段

Cocoon Gem: Edit view is only showing empty form fields

我在 Rails 应用程序中使用 Cocoon 将员工(用户)分配给项目(多对多连接)。关联的创建工作正常,但每次我添加另一个员工时,cocoon 都会在编辑视图中添加一个空的表单字段。 None 编辑视图中的其他 cocoon 表单字段也被填充。这可能是由于使用了下拉菜单 (select)?

当我在浏览器中检查表单时,我可以看到每个字段似乎都分配给了其中一个关联,但 selection 仍然是空的。 Screenshot of my View

我想实现的是,每个关联都显示在一个茧形字段中,以便可以对其进行编辑。提前感谢您的帮助!

我的代码如下(很抱歉有任何混乱,这是我第一次尝试两个模型的多对多连接)。

项目编辑视图

 <%= form_for(@project, :url => project_path, method: :patch) do |f| %>

    <div class="form-group">
        <%= f.label :title %>
        <%= f.text_field :title, class: "form-control" %>
    </div>

    <div class="form-group">
        <%= f.label :customer %>
        <%= f.text_field :customer, class: "form-control" %>
    </div>

    <%= f.fields_for :user_projects do |collab| %>
        <% collab.hidden_field :project_id, value: @project.id %>
        <%= render 'user_project_fields', f: collab %>
    <% end %>
    <div class="add-collaborator">
        <%= link_to_add_association "add", f, :user_projects, class: "btn btn-mmc" %>
    </div>

    <div class="actions">
        <%= f.submit "Save Changes", class: "btn btn-mmc btn-mmc-medium" %>
    </div>
<% end %>

茧场部分

<div class="nested-fields">
    <%= f.label "Select User" %>
    <div class="form-group custom-form-group">
        <%= f.select(:user_id, options_for_select(User.all.map { |u| [u.email, u.id] }), {include_blank: true}, {class: 'form-control'})%>
        <div class="btn-user-project">
            <%= link_to_remove_association "x", f, class: "btn btn-mmc-attention btn-mmc" %>
        </div>
    </div>
</div>

项目模型

class Project < ApplicationRecord
    has_many :user_projects
    has_many :users, :through => :user_projects

    accepts_nested_attributes_for :user_projects, reject_if: :all_blank, allow_destroy: true
end

用户模型

class User < ApplicationRecord
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

    has_many :user_projects
    has_many :projects, :through => :user_projects
end

项目总监

def edit
    @project = Project.find(params[:id])
    @project.user_projects.build
end

def update
    @project = Project.find(params[:id])
    @project.update(project_params)
    @new_collaborator = UserProject.new(user_id: params[:user_id], project_id: params[:project_id])
    @new_collaborator.save
    if @project.update(project_params) && @new_collaborator.save
        redirect_to projects_path
    else
        render :edit
    end
end

private

def project_params
    params.require(:project).permit(:title, :customer, :delted, user_projects_attributes: [:user_id, :project_id]).reject { |_, v| v.blank? }
end

那是因为你创建了两次对象。

第一次:

@project.update(project_params) # from accepts_nested_attributes_for

第二次:

@new_collaborator = UserProject.new(user_id: params[:user_id], project_id: params[:project_id])
@new_collaborator.save

P.s.

你能展示一下project_params方法吗?我想我知道为什么第一个对象为空

我猜测到实际值的映射没有正确完成,例如user_id 的值未标记为选中,在 options_for_select 中您必须添加所选值作为参数(请参阅 documentation)。

但是,有一个更简单的版本:

<%= f.collection_select(:user_id, User.all, :id, :email) %>

顺便说一句,使用像 simple_form 这样的 gem 也可以使构建表单更加直观和直接。