Simple_Form协会的has_many:通过额外的字段 [英] Simple_Form Association with has_many :through extra field
问题描述
我有两个型号,开发人员和任务,
I have two models, Developers and Tasks,
class Developer < ActiveRecord::Base
attr_accessible :address, :comment, :email, :name, :nit, :phone, :web
has_many :assignments
has_many :tasks, :through => :assignments
end
class Task < ActiveRecord::Base
attr_accessible :description, :name, :sprint_id, :developer_ids
has_many :assignments
has_many :developers, :through => :assignments
end
class Assignment < ActiveRecord::Base
attr_accessible :accomplished_time, :developer_id, :estimated_time, :status, :task_id
belongs_to :task
belongs_to :developer
end
IM通过添加分配表,所以我可以添加许多开发商一个特别的任务,照顾关系,现在我也想能够操纵其他领域我加入到加入表像'estimated_time ','accomplished_time'...等...我得到了我的Simple_form是
`
im taking care of the relation by adding an Assignment table, so i can add many developers to one task in particular, now i would also like to be able to manipulate the other fields i added to the joining table like the 'estimated_time', 'accomplished_time'... etc... what i got on my Simple_form is `
<%= simple_form_for [@sprint,@task], :html => { :class => 'form-horizontal' } do |f| %>
<%= f.input :name %>
<%= f.input :description %>
<%= f.association :developers, :as => :check_boxes %>
<div class="form-actions">
<%= f.button :submit, :class => 'btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
project_sprint_path(@sprint.project_id,@sprint), :class => 'btn' %>
</div>
<% end %>`
这个只允许我选择开发商,我希望能够修改estimated_time场就在这里。
This only allows me to select the developers, i want to be able to modify the estimated_time field right there.
有什么建议?
推荐答案
我喜欢简单的形式有着怎样的关联帮手,使得在某些情况下,它可以很方便。不幸的是,你想要什么,你不能只是简单的形式解决。
I love how simple-form has the association helpers, making it really easy in some cases. Unfortunately, what you want you cannot solve with just simple-form.
您必须创建分配
为此工作。
有两种可能的方法。
对于这两个,你将不得不添加以下模型:
For both you will have to add the following to your model:
class Task
accepts_nested_attributes_for :assignments
end
请注意,如果您使用的是 attr_accesible
,您还应该添加 assignments_attributes
来了。
Note that if you are using attr_accesible
, you should also add assignments_attributes
to it.
假设你知道有多少作业,最大限度地,一个任务
会有。假设1为简单。
Suppose you know how many assignments, maximally, a task
would have. Suppose 1 for simplicity.
在你的控制器,写
def new
@task = Task.build
@task.assignments.build
end
这将确保有一个新的任务。
This will make sure there is one new assignment.
在你的看法写:
= simple_form_for [@sprint,@task], :html => { :class => 'form-horizontal' } do |f|
= f.input :name
= f.input :description
= f.simple_fields_for :assignments do |assignment|
= assignment.association :developer, :as => :select
= assignment.estimated_time
.form-actions
= f.button :submit, :class => 'btn-primary'
= link_to t('.cancel', :default => t("helpers.links.cancel")),
project_sprint_path(@sprint.project_id,@sprint), :class => 'btn'
使用这种方法的问题:如果你想要的东西超过1,2或3
The problem with this approach: what if you want more than 1, 2 or 3?
茧是一个宝石,它允许你创建动态嵌套形式。
Cocoon is a gem that allows you to create dynamic nested forms.
您认为会成为:
= simple_form_for [@sprint,@task], :html => { :class => 'form-horizontal' } do |f|
= f.input :name
= f.input :description
= f.simple_fields_for :assignments do |assignment|
= render `assignment_fields`, :f => assignment
.links
= link_to_add_association 'add assignment', f, :assignments
.form-actions
= f.button :submit, :class => 'btn-primary'
= link_to t('.cancel', :default => t("helpers.links.cancel")),
project_sprint_path(@sprint.project_id,@sprint), :class => 'btn'
和定义部分 _assignment_fields.html.haml
:
.nested_fields
= f.association :developer, :as => :select
= f.estimated_time
= link_to_remove_association 'remove assignment', f
希望这有助于。
这篇关于Simple_Form协会的has_many:通过额外的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!