Gas*_*var 12 ruby-on-rails associations has-many-through ruby-on-rails-3 simple-form
我有两个模型,开发人员和任务,
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
Run Code Online (Sandbox Code Playgroud)
我通过添加一个Assignment表来处理关系,所以我可以将许多开发人员添加到一个任务中,现在我也希望能够操作我添加到连接表中的其他字段,如'estimated_time',' completed_time'...等......我在Simple_form上得到的是`
<%= 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 %>`
Run Code Online (Sandbox Code Playgroud)
这只允许我选择开发人员,我希望能够在那里修改estimated_time字段.
有什么建议?
nat*_*vda 27
我喜欢简单形式的协会助手,在某些情况下让它变得非常简单.不幸的是,你想要的只是简单的形式无法解决.
你必须创建assignments这个才能工作.
有两种可能的方法.
对于两者,您必须将以下内容添加到模型中:
class Task
accepts_nested_attributes_for :assignments
end
Run Code Online (Sandbox Code Playgroud)
请注意,如果您正在使用attr_accesible,则还应添加assignments_attributes它.
假设您知道最多可以分配多少个分配task.假设为简单起见1.
在你的控制器中,写
def new
@task = Task.build
@task.assignments.build
end
Run Code Online (Sandbox Code Playgroud)
这将确保有一个新的任务.
在你的视图中写道:
= 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'
Run Code Online (Sandbox Code Playgroud)
这种方法的问题是:如果你想要超过1,2或3怎么办?
Cocoon是一个允许您创建动态嵌套表单的gem.
您的观点将变为:
= 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'
Run Code Online (Sandbox Code Playgroud)
并定义一个部分_assignment_fields.html.haml:
.nested_fields
= f.association :developer, :as => :select
= f.estimated_time
= link_to_remove_association 'remove assignment', f
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
19119 次 |
| 最近记录: |