Rails引导模式客户端验证

时间:2019-03-08 09:21:00

标签: ruby-on-rails ajax

我有一个使用Ajax的引导程序模式:

 <%= link_to 'Question', new_question_path, remote: true, class: 'btn btn-info' %>

_form_modal.html.erb

<div class="modal fade" id="form_modal" tabindex='-1'>
  <%= simple_form_for @question, remote: true, validate: true do |f| %>
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title">Question</h4>
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        </div>
        <div class="modal-body">
          <div class="form-inputs">
            <%= f.input :content, label: false %>
          </div>
        </div>
        <div class="modal-footer">
          <%= f.button :submit, "Ask", class:" btn btn-primary" %>
        </div>
      </div>
    </div>
  <% end %>
</div>

new.js.erb

 $('#remotecontainer').html('<%= j render "questions/form_modal", locals: { product: @product } %>');
 $('#form_modal').on('shown.bs.modal', function() {
 $('form[data-validate]').enableClientSideValidations();
 }).modal();

我正在通过以下方式访问模式:<div id='remotecontainer'></div>

控制器方法:

def new
  @question = Question.new
end


def create
  @question = Question.new(question_params)

  respond_to do |format|
    if @question.save
      format.html { redirect_to root_path, notice: "Question was successfully sent!" }
    else
      format.js { render 'new.js.erb'}
    end
  end
end

我在模型内部有一个验证:validates_length_of :content, maximum: 70

我将以下两个宝石用于验证错误:

gem 'client_side_validations'
gem 'client_side_validations-simple_form'

出现模态,如果我提交的表单少于70个字符,则会创建问题。但是另一方面,如果我输入的字符数超过70个,则会出现验证错误,并且会发生一些奇怪的事情……如果我输入并重新提交少于70个字符的表单,它将继续给我验证错误,但不会请允许我提交表格。我可能在这里错过了一些东西。。。

更新

好吧,我尝试了一下。...我在控制器的create方法中添加了感叹号:if @question.save!,然后在控制台上收到验证错误。验证错误根本没有出现在模态上。然后,当我尝试以少于70个字符的形式提交表单时,它就会通过。因此,据我了解,这两个gems设置有问题。有人可以帮忙吗?

(0.2ms)  ROLLBACK
  ↳ app/controllers/questions_controller.rb:14
Completed 422 Unprocessable Entity in 10ms (ActiveRecord: 1.6ms)

ActiveRecord::RecordInvalid (Validation failed: Content is too long (maximum is 70 characters)):

1 个答案:

答案 0 :(得分:0)

我需要把它放在我的模态中:

        var network = new BasicNetwork();

        network.AddLayer(new BasicLayer(null, true, inputSize));

        network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, inputSize / 6));
        network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, inputSize / 6 / 4));

        network.AddLayer(new BasicLayer(null, false, outputSize));

        network.Structure.FinalizeStructure();
        network.Reset();

这是我的控制器方法:

<div id="formModal">
    <%= render partial: 'layouts/messages' %>
 </div>

以及create.js.erb内部的

def create
  @question = Question.new(question_params)

  respond_to do |format|
    if @question.save
      flash.now[:success] = "Question was successfully sent!"
      format.js { render 'create.js.erb' }
    else
      flash.now[:alert] = "Something went wrong!"
      format.js { render 'create.js.erb' }
    end
  end
end