【问题标题】:Ignoring associated model validations when adding a join table record添加连接表记录时忽略关联的模型验证
【发布时间】:2015-03-18 09:49:34
【问题描述】:

我有一个简单的 3 模型项目,包括测试、问题和测试问题。

问题有两个文本字段,question_name 和 description,它们都是必填字段。

创建测试时,用户应该能够从多选下拉列表中选择预先填写的问题。

问题是问题模型中的验证阻止用户添加新测试,显示警告通知,说明问题描述可能不是空白。

在这种情况下,当您添加链接到多个问题的测试但保留验证以便通过 QuestionsController 添加新问题时,是否需要绕过模型验证?

这是我的代码:

型号:

class Question < ActiveRecord::Base
  has_many :test_questions
  has_many :tests, through: :test_questions

  validates :question_name, presence: true
  validates :description, presence: true
end

class TestQuestion < ActiveRecord::Base
  belongs_to :test
  belongs_to :question
end

class Test < ActiveRecord::Base
  has_many :test_questions
  has_many :questions, through: :test_questions      
  accepts_nested_attributes_for :questions
end

tests_controller.rb

class TestsController < ApplicationController
  before_action :set_test, only: [:show, :edit, :update, :destroy]

  def index
    @tests = Test.all
  end

  def new
    @test = Test.new
    @test.questions.build
  end

  def create
    @test = Test.new(test_params)   
    respond_to do |format|
      if @test.save
        format.html { redirect_to @test, notice: 'Test was successfully created.' }
        format.json { render :show, status: :created, location: @test }
      else
        format.html { render :new }
        format.json { render json: @test.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @test.update(test_params)
        format.html { redirect_to @test, notice: 'Test was successfully updated.' }
        format.json { render :show, status: :ok, location: @test }
      else
        format.html { render :edit }
        format.json { render json: @test.errors, status: :unprocessable_entity }
      end
    end
  end

  private

    def set_test
      @test = Test.find(params[:id])
    end

    def test_params
      params.require(:test).permit(:test_name, question_ids: [])
    end
end

_form.html.erb

<%= form_for(@test) do |f| %>
  <% if @test.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@test.errors.count, "error") %> prohibited this test from being saved:</h2>

      <ul>
      <% @test.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="form-group">
  <%= f.label :test_name %>
  <%= f.text_field :test_name %>
  </div>

  <div class="form-group">  
  <%= f.collection_select :question_ids, Question.all, :id, :question_name, {}, {multiple: true, class: "form-control"} %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

【问题讨论】:

    标签: validation ruby-on-rails-4 activerecord


    【解决方案1】:

    没关系,我找到了解决方案:使用 on: 指定要验证的方法

    validates :description, presence: true, on: [:create, :edit]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-18
      • 1970-01-01
      • 2014-08-30
      • 2020-12-02
      • 2019-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多