如何在rails 5上的ruby中使用select选项创建一个创建表单

问题描述 投票:-1回答:1

我需要在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
ruby-on-rails ruby
1个回答
0
投票

您需要使用嵌套表单字段来处理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,它允许您动态添加和删除待办事项。

© www.soinside.com 2019 - 2024. All rights reserved.