Rails 关系和形式
Rails relationship and forms
这个问题让我有些困惑。这是我的用户故事:
"John need to create a (issue) task for the maintenance team with the train name and number, the parts that have problems, the priority and who is responsible for that task."
每个问题可以有一个或多个解决方案,每个部分可以有一个问题。
火车可能有很多问题(任务),但不是同一个问题。
所以,基于此我创建了那些
class Issue < ActiveRecord::Base
belongs_to :project
has_many :train_issues, :dependent => :destroy
accepts_nested_attributes_for :train_issues, :reject_if => lambda { |a| a[:content].blank? }
end
class TrainIssue < ActiveRecord::Base
belongs_to :issue
has_many :train_displays
has_many :train_display_problems
accepts_nested_attributes_for :train_display_problems, :reject_if => lambda { |a| a[:content].blank? }
end
class TrainDisplay < ActiveRecord::Base
belongs_to :train_car
belongs_to :train_issue
end
class TrainProblemAndSolution < ActiveRecord::Base
belongs_to :TrainDisplayProblem
belongs_to :TrainDisplaySolution
end
我还有 Train.rb 和 TrainCar.rb,它们都是相关的,而且工作正常。我可以通过显示找到 train_id,反之亦然。
我的问题是,如何为该用户创建一个表单来创建所有这些信息的问题?
我阅读并使用 nestedForms 做了一些示例,但效果不佳,表单创建了一个问题,但没有使用参数创建 TrainIssue。
非常感谢!
您可以对嵌套属性使用 Formtastic。它允许简单地为它们添加一个子表单:
<%= semantic_form_for @issue do |f| %>
<%= f.inputs :title, :description %>
<%= f.inputs :for => :trains_issues do |train_issue, index| %>
<%= train_issue.input :name %>
...
<%= end %>
<%= f.actions %>
<% end %>
这个问题让我有些困惑。这是我的用户故事:
"John need to create a (issue) task for the maintenance team with the train name and number, the parts that have problems, the priority and who is responsible for that task."
每个问题可以有一个或多个解决方案,每个部分可以有一个问题。 火车可能有很多问题(任务),但不是同一个问题。
所以,基于此我创建了那些
class Issue < ActiveRecord::Base
belongs_to :project
has_many :train_issues, :dependent => :destroy
accepts_nested_attributes_for :train_issues, :reject_if => lambda { |a| a[:content].blank? }
end
class TrainIssue < ActiveRecord::Base
belongs_to :issue
has_many :train_displays
has_many :train_display_problems
accepts_nested_attributes_for :train_display_problems, :reject_if => lambda { |a| a[:content].blank? }
end
class TrainDisplay < ActiveRecord::Base
belongs_to :train_car
belongs_to :train_issue
end
class TrainProblemAndSolution < ActiveRecord::Base
belongs_to :TrainDisplayProblem
belongs_to :TrainDisplaySolution
end
我还有 Train.rb 和 TrainCar.rb,它们都是相关的,而且工作正常。我可以通过显示找到 train_id,反之亦然。
我的问题是,如何为该用户创建一个表单来创建所有这些信息的问题? 我阅读并使用 nestedForms 做了一些示例,但效果不佳,表单创建了一个问题,但没有使用参数创建 TrainIssue。
非常感谢!
您可以对嵌套属性使用 Formtastic。它允许简单地为它们添加一个子表单:
<%= semantic_form_for @issue do |f| %>
<%= f.inputs :title, :description %>
<%= f.inputs :for => :trains_issues do |train_issue, index| %>
<%= train_issue.input :name %>
...
<%= end %>
<%= f.actions %>
<% end %>