无法在 rails 关联中呈现表单
not able to render form in rails association
我创建了一个项目脚手架,该脚手架与阶段脚手架具有多对一关联,现在我创建了一个任务脚手架,该任务脚手架与阶段脚手架具有多对一关联。
但是我的任务表单没有呈现我收到错误。
routes.rb
resources :projects do
resources :stages do
resources :tasks
end
end
任务form.html.erb
<%= form_with(model: task, url: [@stages, task], local: true) do |form| %>
<% if task.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(task.errors.count, "error") %> prohibited this task from being saved:</h2>
<ul>
<% task.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field columns large-6">
<%= form.label :task_name %>
<%= form.text_field :task_name %>
</div>
<div class="actions">
<%= form.submit 'Create', :class=>'button primary small' %>
</div>
<% end %>
型号project.rb
has_many :stages
型号stage.rb
belongs_to :project
has_many :tasks
型号task.rb
belongs_to :stage
Rails 在您的应用程序中找不到 tasks_path,因为您将任务移到了 projects
和 stages
资源下,所以 tasks_path 的完整路径看起来像 projects_stages_tasks_path
因此,您需要为给定的项目和阶段
指定此url
<%= form_with(model: task, url: projects_stages_tasks_path(@stages, @stages.project), local: true) do |form| %>
我创建了一个项目脚手架,该脚手架与阶段脚手架具有多对一关联,现在我创建了一个任务脚手架,该任务脚手架与阶段脚手架具有多对一关联。 但是我的任务表单没有呈现我收到错误。
routes.rb
resources :projects do
resources :stages do
resources :tasks
end
end
任务form.html.erb
<%= form_with(model: task, url: [@stages, task], local: true) do |form| %>
<% if task.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(task.errors.count, "error") %> prohibited this task from being saved:</h2>
<ul>
<% task.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field columns large-6">
<%= form.label :task_name %>
<%= form.text_field :task_name %>
</div>
<div class="actions">
<%= form.submit 'Create', :class=>'button primary small' %>
</div>
<% end %>
型号project.rb
has_many :stages
型号stage.rb
belongs_to :project
has_many :tasks
型号task.rb
belongs_to :stage
Rails 在您的应用程序中找不到 tasks_path,因为您将任务移到了 projects
和 stages
资源下,所以 tasks_path 的完整路径看起来像 projects_stages_tasks_path
因此,您需要为给定的项目和阶段
指定此url<%= form_with(model: task, url: projects_stages_tasks_path(@stages, @stages.project), local: true) do |form| %>