我创建了一个脚手架名称项目,并创建了另一个名为 阶段。项目与阶段之间存在一对多的联系。就像每个项目都会有多个阶段。我可以呈现舞台表单,但是无法将数据保存到数据库的舞台表中。 this error i get on saving stage form
stage form.html.erb
<%= form_with(model: stage, url: [@project, stage], local: true) do |form| %>
<% if @stage.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(stage.errors.count, "error") %> prohibited this stage from being saved:</h2>
<ul>
<% stage.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="row select-date-wrapper">
<div class="field columns large-6">
<%= form.label :stage %>
<%= form.text_field :stage %>
</div>
<div class="field columns large-3">
<%= form.label :planned_start_date %>
<%= form.date_select :planned_start_date, class: 'select-date' %>
</div>
<div class="actions">
<%= form.submit 'Create', :class=>"button primary small" %>
</div>
<% end %>
stage_controller.rb
def index
@stages = Stage.all
end
def new
@stage = Stage.new
@project = Project.find(params[:project_id])
end
def create
@project = Project.find(params[:project_id])
@stage = @project.stages.build(stage_params)
respond_to do |format|
if @stage.save
format.html { redirect_to @stage, notice: 'Stage was successfully created.' }
format.json { render :show, status: :created, location: @stage }
else
format.html { render :new }
format.json { render json: @stage.errors, status: :unprocessable_entity }
end
end
end
模型project.rb
has_many :stages
model stage.rb
#belongs_to :project
has_many :tasks
routes.rb
resources :projects do
resources :stages
end
答案 0 :(得分:0)
您尝试过吗?
if @stage.save
format.html { redirect_to project_stage_path(@project, @stage), notice: 'Stage was successfully created.' }
format.json { render :show, status: :created, location: @stage }
else
format.html { render :new }
format.json { render json: @stage.errors, status: :unprocessable_entity }
end
答案 1 :(得分:0)
请从url: [@project, stage]
更改为url: stages_path
始终尝试使控制器名称复数。 stages_controller