gpt4 book ai didi

ruby-on-rails - 如何防止在使用嵌套表单和 rails 4 时创建空白?

转载 作者:数据小太阳 更新时间:2023-10-29 08:46:47 24 4
gpt4 key购买 nike

我有一个嵌套表单 question.erb,它显示预先写好的问题 (:poll),然后有用于答案的 text_fields (:response)。我遇到的问题是在数据库中创建了空白问题。我对这个问题做了相当多的研究,看来答案是添加

accepts_nested_attributes_for :questions, reject_if: proc { |attributes| attributes['poll'].blank? }

事件模型。这个问题当然是现在我正在阻止所有内容被写入。我将如何只将答案写入数据库并忽略(或根本不创建)空白问题?如果解决方案可能需要任何其他文件,请告诉我。

谢谢!

问题.erb

<%= simple_form_for(@event) do |f| %>
<%= f.error_notification %>
<%= f.object.name %>
<%= f.simple_fields_for :questions, @event.questions do |q| %>
<%= q.object.poll%>
<%= q.simple_fields_for :answers, q.object.answers.build do |a|%>
<%= a.text_field :response %>
<% end %>
<% end %>
<%= f.button :submit%>
<% end %>

事件.rb

class Event < ActiveRecord::Base
has_many :questions
accepts_nested_attributes_for :questions
end

events_controller.rb(部分)

 def question
@event = Event.find(params[:id])
end

def update
@event = Event.find(params[:id])
if @event.update(event_params)
redirect_to events_path, notice: "Answers saved"
else
redirect_to events_question_path, notice: "Answers not saved"
end
end

def event_params
params.require(:event).permit(
questions_attributes: [:poll, answers_attributes: [:response]])
end

问题.rb

 class Question < ActiveRecord::Base

belongs_to :user
belongs_to :event

has_many :answers


accepts_nested_attributes_for :answers
end

问题 Controller .rb

class QuestionsController < ApplicationController
before_action :set_question, only: [:show, :edit, :update, :destroy]

def index
@questions = Question.all
end

def show
end

def new
@question = Question.new

end


def edit
end

def create
@question = Question.new(question_params)

@question.user = current_user

respond_to do |format|
if @question.save
format.html { redirect_to @question, notice: 'Question was successfully created.' }
format.json { render :show, status: :created, location: @question }
else
format.html { render :new }
format.json { render json: @question.errors, status: :unprocessable_entity }
end
end
end

def update
@question.user = current_user.id
respond_to do |format|
if @question.update(question_params)
format.html { redirect_to @question, notice: 'Question was successfully updated.' }
format.json { render :show, status: :ok, location: @question }
else
format.html { render :edit }
format.json { render json: @question.errors, status: :unprocessable_entity }
end
end
end

# DELETE /questions/1
# DELETE /questions/1.json
def destroy
@question.destroy
respond_to do |format|
format.html { redirect_to questions_url }
format.json { head :no_content }
end
end

private

def set_question
@question = Question.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def question_params
params.require(:question).permit(:poll, :event_id, answer_attributes: [:response, :question, :event, :user, :event_id, :question_id, :user_id])
end
end

最佳答案

试试这个:

class Event < ActiveRecord::Base
has_many :questions
accepts_nested_attributes_for :questions, reject_if: :all_blank
end

您可以在 the official docs 上阅读更多相关信息,但简而言之,这将阻止保存任何完全空白的问题,并让任何填充的问题通过。

关于ruby-on-rails - 如何防止在使用嵌套表单和 rails 4 时创建空白?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25615907/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com