其中一些如何显示的相关记录子集的形式,不存在了吗? [英] How to display a form for a subset of associated records, some of which don't exist yet?

查看:101
本文介绍了其中一些如何显示的相关记录子集的形式,不存在了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有任务和用户。当用户完成一个任务,我创建一个完成它具有为用户指示多久,他们花了现场。我需要一个表格,显示所有与他们的完成状态和time_spent属性的任务。在提交,存在完工应该更新,并应创建新的。我想如果可能的话,为此在Formtastic但我会很乐意与一个基本的Rails 3的解决方案。

I have Tasks and Users. When a user completes a task, I create a Completion which has a field for the user to indicate how long they spent. I need a form that shows all the tasks with their completion status and time_spent attribute. On submit, completions that existed should be updated and new ones should be created. I'd like to do this in Formtastic if possible but I'll be happy with a basic Rails 3 solution.

class Completion < ActiveRecord::Base
  belongs_to :task
  belongs_to :user

  # attribute time_spent
end

class User < ActiveRecord::Base
  has_many :completions
  has_many :tasks, :through => :completions
end    

class Task < ActiveRecord::Base
  belongs_to :milestone
  has_many :completions
  has_many :users, :through => :completions
end

这是额外的方面是,我想只显示一组特定的任务,比如那些属于一个里程碑。我应该有里程碑控制器的形式,帖子到完井控制器?

An extra aspect is that I want to show just a certain set of tasks, such as those belonging to a Milestone. Should I have a form on the Milestone controller that posts to the Completions controller?

class Milestone < ActiveRecord::Base
  has_many :tasks
  has_many :completions, :through => :tasks
end

更新 现在我已经看了好几天,我发现许多 <一href="http://stackoverflow.com/questions/2045563/rails-has-many-through-association-and-the-form-for-creating-new-instances">dead <一href="http://stackoverflow.com/questions/3714014/use-accepts-nested-attributes-for-to-create-new-records-or-update-existing">ends. 这 href="http://stackoverflow.com/questions/972857/multiple-objects-in-a-rails-form">多个对象接近,但它要求所有的链接对象已经存在。

UPDATE I've looked for days now and I've found many dead ends. This Multiple objects in a Rails form is close, but it requires that all the linking objects already exist.

是什么使这个问题与众不同的是,有些链接不存在,但并没有为嵌套链接到没有单一的模式。例如:随着href="http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes" rel="nofollow">嵌套瑞安英对照精华文章的)我在编辑所有可能的完成了用户的形式使这一工作,但我需要编辑尽可能完整的子集的一种形式。我需要做一个冗余的对象MilestoneCompletions的的has_many 完成和 belongs_to的用户?可以在加载ActiveModel 的has_many

What sets this question apart is that some of the links don't exist yet and there is no single model for the links to be nested in. E.g. With Ryan Daigle's Nested Object Forms post) I've made this work in a form to edit all possible Completions for a user, but I need to edit a subset of possible Completions in one form. Do I need to make a redundant object MilestoneCompletions that has_many Completions and belongs_to User? Can an ActiveModel has_many?

推荐答案

我终于解决了这个。其中一个关键是收集参数 fields_for 。另一种是,以产生集合与现有的和新记录的混合。

I finally solved this. One key is the collection argument to fields_for. The other is to generate the collection with a mix of existing and new records.

所以,在视图中,是这样的:

So in the view, something like:

<%= form_for @user do |f| %>
  <table>
    <tr><th>Completed</th><th>Time spent</th><th>Task</th></tr>

    <%= f.fields_for :completions, available_completions_for_milestone(@user, @milestone) do |cf| %>
      <tr>
        <td><%= cf.check_box :status, {disabled: cf.object.persisted?}, "done", "unreported" %></td>
        <td><%= cf.text_field :time_spent_text %></td>
        <td><%= cf.object.task.description %></td>
      </tr>
      <%= cf.hidden_field :task_id %>
    <% end -%>

通过一个辅助方法:

def available_completions_for_milestone(user, milestone)
  user_completions = user.completions.in_milestone(milestone)    
  available = []
  milestone.tasks.each do |t|
    c = user_completions.select{|c| c.task_id == t.id}.first
    if !c then # make it
      c = user.completions.build( task: t )
    end
    available << c
  end
  available
end

在完井已在数据库中进行检查和残疾人,使他们不能被选中的观点通知。在未选中状态得到值未报告和用户模式可以过滤掉这些记录,使他们不走在DB:

Notice in the view that completions already in the DB are checked and disabled so they can't be unchecked. The unchecked state gets the value "unreported" and the User model can filter out those records so they don't go in the DB:

has_many :completions
accepts_nested_attributes_for :completions, :reject_if => proc { |attrs| attrs['status'] == 'unreported' }

我也不得不做出 completions_attributes attr_accessible上的用户模型。如果您 task_ids 访问则更新将删除被冷落的把那落成

I also had to make completions_attributes attr_accessible on the User model. If you make task_ids accessible then update will delete completions that were left out of the PUT.

这篇关于其中一些如何显示的相关记录子集的形式,不存在了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆