Ruby on rails Rails 6:嵌套表单未保存

Ruby on rails Rails 6:嵌套表单未保存,ruby-on-rails,ruby-on-rails-6,nested-attributes,Ruby On Rails,Ruby On Rails 6,Nested Attributes,我一直在尝试在Rails 6中运行一个嵌套表单的简单测试示例,但到目前为止,我还无法创建一个有效的示例。据我所知,我在模型中创建了正确的关联,添加了嵌套表单,允许参数接受嵌套属性。 每当我提交表单时,我都会得到已完成的422不可处理实体,这是我遇到了一些麻烦,不知道该怎么做才能继续 投票模式 class Poll < ApplicationRecord has_many :questions accepts_nested_attributes_for :questions, all

我一直在尝试在Rails 6中运行一个嵌套表单的简单测试示例,但到目前为止,我还无法创建一个有效的示例。据我所知,我在模型中创建了正确的关联,添加了嵌套表单,允许参数接受嵌套属性。 每当我提交表单时,我都会得到
已完成的422不可处理实体
,这是我遇到了一些麻烦,不知道该怎么做才能继续

投票模式

class Poll < ApplicationRecord
  has_many :questions
  accepts_nested_attributes_for :questions, allow_destroy: true, reject_if: :all_blank
  validates_presence_of :title
end
class Question < ApplicationRecord
  belongs_to :poll
  validates_presence_of :content, :poll_id
end

在运行
问题
模型中的验证时,不会创建
轮询
,因此问题的
轮询id
,导致状态验证失败。您可以删除验证,因为
属于:poll
关联验证该轮询是否存在()

课堂问题
在运行
问题
模型中的验证时,不会创建
轮询
,因此问题的
轮询id
,导致状态验证失败。您可以删除验证,因为
属于:poll
关联验证该轮询是否存在()

课堂问题
你能在
else
块中添加
p@poll.errors.full\u消息并共享输出吗?因此它给出
[“问题轮询不能为空”]
你能在
else
块中添加
p@poll.errors.full\u消息并共享输出吗?因此它给出
[“问题轮询不能为空”
class PollsController < ApplicationController
before_action :set_poll, only: %i[ show edit update destroy ]

# GET /polls or /polls.json
  def index
  @polls = Poll.all
end

# GET /polls/1 or /polls/1.json
def show
end

# GET /polls/new
def new
  @poll = Poll.new
  2.times { @poll.questions.build}
end

# GET /polls/1/edit
  def edit
end

# POST /polls or /polls.json
def create
  @poll = Poll.new(poll_params)
  respond_to do |format|
    if @poll.save
     format.html { redirect_to @poll, notice: "Poll was successfully created." }
     format.json { render :show, status: :created, location: @poll }
    else
     # raise poll_params.inspect  
     format.html { render :new, status: :unprocessable_entity }
     format.json { render json: @poll.errors, status: :unprocessable_entity }
   end
 end
end

# PATCH/PUT /polls/1 or /polls/1.json
def update
  respond_to do |format|
    if @poll.update(poll_params)
      format.html { redirect_to @poll, notice: "Poll was successfully updated." }
      format.json { render :show, status: :ok, location: @poll }
    else
      format.html { render :edit, status: :unprocessable_entity }
      format.json { render json: @poll.errors, status: :unprocessable_entity }
    end
  end
end

# DELETE /polls/1 or /polls/1.json
def destroy
 @poll.destroy
 respond_to do |format|
   format.html { redirect_to polls_url, notice: "Poll was successfully destroyed." }
   format.json { head :no_content }
 end
end

private
  # Use callbacks to share common setup or constraints between actions.
 def set_poll
   @poll = Poll.find(params[:id])
 end

 # Only allow a list of trusted parameters through.
 def poll_params
   params.require(:poll).permit(:title, :questions_attributes => [:id, :content, :_destroy] )
 end
end
<%= form_for(@poll) do |f| %>
...

<fieldset>
<div>
    <%= f.label :title %>
    <%= f.text_field :title %>
</div>

    <legend>Questions:</legend>
    <%= f.fields_for :questions do |questions_form| %>
        <%= render "question_fields", f: questions_form %>
    <% end %>

    <%= link_to_add_fields "Add Questions", f, :questions %>
    <input name="authenticity_token" 
           type="hidden" 
           value="<%= form_authenticity_token %>"/>
</fieldset>

<%= f.submit %>
<% end %>
<div class="nested-fields">
<%= f.hidden_field :_destroy %>
<div>
    <%= f.label :content %>
    <%= f.text_field :content %>
</div>
<div>
    <%= link_to "Remove", '#', class: "remove_fields" %>
</div>
</div>
Started POST "/polls" for ::1 at 2021-04-06 16:03:15 +0900
Processing by PollsController#create as HTML
Parameters: {"authenticity_token"=>"[FILTERED]", "poll"=>{"title"=>"Test Poll", 
"questions_attributes"=>{"0"=>{"_destroy"=>"false", "content"=>"Question 1"}, "1"=> 
{"_destroy"=>"false", "content"=>"Question 2"}}}, "commit"=>"Create Poll"}
Rendering layout layouts/application.html.erb
Rendering polls/new.html.erb within layouts/application
Rendered polls/_question_fields.html.erb (Duration: 0.5ms | Allocations: 296)
Rendered polls/_question_fields.html.erb (Duration: 0.4ms | Allocations: 294)
Rendered polls/_question_fields.html.erb (Duration: 0.7ms | Allocations: 285)
Rendered polls/_form.html.erb (Duration: 4.1ms | Allocations: 1695)
Rendered polls/new.html.erb within layouts/application (Duration: 4.5ms | Allocations: 1804)
[Webpacker] Everything's up-to-date. Nothing to do
Rendered layout layouts/application.html.erb (Duration: 92.4ms | Allocations: 8240)
Completed 422 Unprocessable Entity in 115ms (Views: 93.4ms | ActiveRecord: 0.0ms | Allocations: 10383)
class Question < ApplicationRecord
  belongs_to :poll
  validates_presence_of :content
end