Rails 3 has_many:通过带有下拉菜单的表单?
我正在尝试在 Rails 3(使用 Formtastic)中实现 has_many :through join ,但我有点卡住了。我的模型设置如下:
模型:
class Project < ActiveRecord::Base
has_many :employees, :through => :teams
has_many :teams
class Employee < ActiveRecord::Base
has_many :projects, :through => :teams
has_many :teams
class Team < ActiveRecord::Base
belongs_to :project
belongs_to :employee
这一行在项目视图中为我提供了一个多选框,允许选择员工:
视图:
<%= f.input :employees, :as => :select %>
到目前为止这样就完成了工作,但我想要的是一个单独的下拉框来选择每个员工的姓名,然后选择他们在项目中的角色。我无法弄清楚到达那里的表单代码...
编辑:
按照建议,我从 Railscast 197:嵌套模型表单正在工作,并且已经完成了一半。这就是我在视图中的内容:
<%= f.semantic_fields_for :employees do |builder| %>
<%= render 'employee_fields', :f => builder %>
<% end %>
<%= link_to_add_fields "add employee", f, :employees %>
和“employee_fields”部分:
<p class="fields">
<%= f.input :name, :as => :select, :collection => Employee.find(:all) %>
<%= f.hidden_field :_destroy %>
<%= link_to_remove_fields "remove", f %>
</p>
但现在这会创建一个新员工而不是一个新团队(项目员工加入记录),所以我认为它充当 has_many 而不是
has_many :through
。如何编辑此内容以便上面的 :name
输入将记录添加到 project[employee_ids][]
中?
I'm trying to implement a has_many :through join in Rails 3 (with Formtastic) and I'm a little stuck. I have the models set up as follows:
Models:
class Project < ActiveRecord::Base
has_many :employees, :through => :teams
has_many :teams
class Employee < ActiveRecord::Base
has_many :projects, :through => :teams
has_many :teams
class Team < ActiveRecord::Base
belongs_to :project
belongs_to :employee
And this line gives me a multi-select box in the projects view that allows employees to be selected:
View:
<%= f.input :employees, :as => :select %>
So far this gets the job done, but what I'd LIKE to have is a separate dropdown box to select each employee's name, then their role in the project. I can't figure out the form code to get me there...
EDIT:
As suggested, I've gotten the code from Railscast 197: Nested Model Forms working and it's part-way there. This is what I have in the view:
<%= f.semantic_fields_for :employees do |builder| %>
<%= render 'employee_fields', :f => builder %>
<% end %>
<%= link_to_add_fields "add employee", f, :employees %>
and the 'employee_fields' partial:
<p class="fields">
<%= f.input :name, :as => :select, :collection => Employee.find(:all) %>
<%= f.hidden_field :_destroy %>
<%= link_to_remove_fields "remove", f %>
</p>
But right now this creates a new employee rather than a new team (project-employee join record), so I think it's acting as a has_many
rather than a has_many :through
. How can I edit this so that the :name
input above adds a record to project[employee_ids][]
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
天哪,我终于让这个东西发挥作用了。以下是相关代码,减去为使表单动态添加和删除字段而添加的位:
_form.html.erb
_team_fields.html.erb
关键是添加
手动输入行,因为无论出于何种原因,这都没有作为一部分生成的形式。这使得表单真正开始更新内容,然后我只需使嵌套表单引用加入模型(
team
)而不是employees
,以便更新我们去对了地方。现在看起来很简单!
Oh my god, I finally got this thing to work. Here's the relevant code, minus the bits added to make the form add and remove fields dynamically:
_form.html.erb
_team_fields.html.erb
The key was adding the
<input id="project_teams_none" name="team[employee_ids][]" type="hidden" value="" />
line in manually, because for whatever reason this wasn't generated as part of the form. This got the form to actually start updating things, and then I just had to make the nested form refer to the join model (team
) rather than toemployees
so that the updates were going to the right place.Looks so simple now!