我需要在qazxsw poi上使用select选项创建嵌套属性qazxsw poi的创建形式,它必须在todo
这里是projects_controller.rb
project
model.rb和todo.rb
/projects/index.html.erb
的routes.rb
def index
@projects = Project.all
@todo = Todo.new
end
和class Project < ApplicationRecord
has_many :todos, inverse_of: :project
accepts_nested_attributes_for :todos, reject_if: proc { |attributes|
attributes[:title].blank? }, allow_destroy: true
end
class Todo < ApplicationRecord
belongs_to :project, inverse_of: :todos
end
root 'projects#index'
resources :projects do
resources :todos
end
您需要使用嵌套表单字段来处理has_many关联。 Rails为您提供了/projects/index.html.erb
助手。
我已根据您的用例调整了他们的文档:
<h1>Tasks</h1>
<table>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
<% for project in @projects %>
<tr><strong><%= project.title %></strong></tr>
<% for todo in project.todos %>
<ul>
<li><%= todo.text %></li>
</ul>
<% end %>
<% end %>
您还可以使用fields_for
等gem,它允许您动态添加和删除待办事项。