无法在 Rails 关联中呈现表单 [英] not able to render form in rails association
问题描述
我创建了一个与舞台脚手架多对一关联的项目脚手架,现在我创建了一个与舞台脚手架多对一关联的任务脚手架.但我的任务表单没有呈现我收到错误.
routes.rb
资源:项目做资源:阶段做资源:任务结尾结尾
任务 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") %>禁止保存此任务:</h2><ul><% task.errors.full_messages.each do |message|%><li><%=消息%></li><%结束%>
<%结束%><div class="field columns large-6"><%= form.label :task_name %><%= form.text_field :task_name %>
<div class="actions"><%= form.submit '创建', :class=>'buttonprimary small' %>
<%结束%>
模型项目.rb
has_many :stages
模型stage.rb
belongs_to :projecthas_many :任务
模型任务.rb
belongs_to :stage
Rails 在您的应用程序中找不到 tasks_path,因为您将任务移到了 projects
和 stages
资源下,所以 tasks_path 的完整路径看起来像 projects_stages_tasks_path
因此,您需要为给定的项目和阶段指定此网址
<%= form_with(model: task, url: projects_stages_tasks_path(@stages, @stages.project), local: true) do |form|%>
i created a project scaffold that has many to one association with stage scaffold now i created a task scaffold that has many to one association with stage scffold. but i task form is not rendered i am getting error.
routes.rb
resources :projects do
resources :stages do
resources :tasks
end
end
Task 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 %>
model project.rb
has_many :stages
model stage.rb
belongs_to :project
has_many :tasks
model task.rb
belongs_to :stage
Rails can't find tasks_path in your application, because you moved tasks under projects
and stages
resources, so full path to tasks_path looks like projects_stages_tasks_path
So, you need to specify this url for given projects and stages
<%= form_with(model: task, url: projects_stages_tasks_path(@stages, @stages.project), local: true) do |form| %>
这篇关于无法在 Rails 关联中呈现表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!