gpt4 book ai didi

ruby-on-rails - rails 与嵌套表单 link_to_add 的多对多关系不起作用

转载 作者:行者123 更新时间:2023-12-04 14:58:21 27 4
gpt4 key购买 nike

我正在尝试构建用于添加议程的复杂表单,我想使用 link_to_add helper 为表单中的给定议程动态添加新用户。使用 nested_form gem 和 accepts_nested_attributes_for 方法。这些是我的模型:

class Agenda < ActiveRecord::Base
has_many :roles
has_many :users, through: :roles
accepts_nested_attributes_for :roles

belongs_to :owner, class_name: User

attr_accessible :name,
:address,
:owner,
:roles_attributes

end


class Role < ActiveRecord::Base
belongs_to :agenda
belongs_to :user
accepts_nested_attributes_for :user

attr_accessible :agenda,
:role,
:user

end

class User < ActiveRecord::Base
has_many :roles
has_many :agendas, through: :roles
end

和我的 Controller :

class AgendasController < ApplicationController

...

def new
@agenda = Agenda.new
@role = @agenda.roles.build
@user = @role.build_user
end
end

和我的表格:

<%= nested_form_for @agenda do |f| %>
....
<%= f.fields_for :roles do |role_f| %>
<%= role_f.fields_for :user do |user_f| %>
<div class="control-group">
<%= user_f.label :email, :class=>"control-label" %>
<div class="controls controls-row">
<%= user_f.text_field :email %>
</div>
</div>
<% end %>
<div class="control-group">
<%= role_f.label :role, :class=>"control-label" %>
<div class="controls controls-row">
<%= role_f.text_field :role %>
</div>
</div>
<%= role_f.link_to_remove "Remove user" %>
<% end %>
<%= f.link_to_add "Add new users", :roles %>

<% end %>

一切正常,除了 link_to_add,它不附加 fields_for :user block 。如何使 link_to_add 帮助程序与 fields_for :user block 一起呈现整个 fields_for :roles block ?

最佳答案

我添加了一个新补丁来制作 this answer与最新版本的嵌套表单相关。现在用这个对我有用..

关于ruby-on-rails - rails 与嵌套表单 link_to_add 的多对多关系不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18259539/

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