使用remote时,仅需要使用jquery-form-validator-rails:true

时间:2017-02-24 11:08:45

标签: javascript jquery ruby-on-rails ruby ajax

所以我最近实施了一个电话/ sim检查器。第一种表格输入要求用户输入他们的电话号码。如果数据库中存在电话号码,则显示找到的电话号码信息,否则将用电话号码表格替换为电话号码表格。再次,如果数据库中存在SIM号,则呈现找到的SIM号码消息,否则呈现未找到SIM号码的消息。

我目前遇到的问题是我添加了gem:jquery-form-validator-rails(https://github.com/gustaflindqvist/jquery-form-validator-rails)并进行了适当的设置。按照有关如何验证字段的文档,如果没有输入任何内容,我可以获取表单以显示消息。但是,当我尝试验证其最小长度时,它不起作用,它响应控制器方法而不是检查验证。 此外,验证甚至不适用于SIM号码表格:app / views / phones / _sim.html.erb

实施以下代码: 应用程序/模型/ phone.rb

class Phone < ActiveRecord::Base
  validates :phone_number, length: {minimum: 11, maximum: 11}, allow_blank: false
  validates :sim_number, length: {minimum: 12, maximum: 12}, allow_blank: false
end

应用程序/控制器/ phones_controller.rb

class PhonesController < ApplicationController
  def checkphone
    @phone_number = Phone.where(phone_number: params[:phone][:phone_number])
    respond_to do |format|
      if @phone_number.exists?
        format.js {render 'phone-found'}
      elsif @phone_number.blank?
        format.js {render 'phone-not-found'}
      else
        format.js {render 'errors'}
      end
    end
  end

  def checksim
    @sim_number = Phone.where('sim_number = ?', params[:sim][:sim_number])
    respond_to do |format|
      if @sim_number.exists?
        format.js {render 'sim-found'}
      elsif @sim_number.blank?
        format.js {render 'sim-not-found'}
      else
        format.js {render 'errors'}
      end
    end
  end

  private

  def phone_params
    params.require(:phone).permit(
      :phone_number
    )
  end

  def sim_params
    params.require(:sim).permit(
      :sim_number
    )
  end
end

应用程序/视图/电话/ index.html.erb

<div id="phone-number-found"></div>
<div id="phone-number-not-found"></div>
<div id="error"></div>
<%= form_for :phone, :url => url_for(:action => 'checkphone', :controller => 'phones'), remote: true, html: { id: 'phone-number-form'}  do |f| %>
  <%= f.label "Phone Number:" %>
  <%= f.number_field(:phone_number, class: "field", data: {
    :validation => "required validate_max_length length11",
    "validation-error-msg" => "This field is required, must only be numbers and be a minimum length of 11 characters"
    }) %>
  <%= submit_tag("Check") %>
<% end %>

应用程序/视图/电话/电话found.js.erb

$('#phone-number-found').html('Phone Number Found!');
$('#phone-number-not-found').html('');
$('#error').html('');
$('#phone-number-form').hide();

应用程序/视图/电话/电话未found.js.erb

$('#phone-number-found').append("<%= j render(partial: 'sim') %>")
$('#phone-number-not-found').html('Phone Number Not Found!');
$('#error').html('');
$('#phone-number-form').hide();

应用程序/视图/电话/ _sim.html.erb

<div id="sim-number-found"></div>
<div id="sim-number-not-found"></div>
<div id="error"></div>
<%= form_for :sim, :url => url_for(:action => 'checksim', :controller => 'phones'), remote: true, html: { id: 'sim-number-form'}  do |f| %>
  <%= f.label "Sim Number:" %>
  <%= f.number_field(:sim_number, class: "field", data: {
    :validation => "required validate_max_length length12",
    "validation-error-msg" => "This field is required, must only be numbers and be a minimum length of 12 characters"
    }) %>
  <%= submit_tag("Check") %>
<% end %>

应用程序/视图/电话/ SIM-found.js.erb

$('#phone-number-found').html('Sim Found')
$('#phone-number-not-found').html('');
$('#phone-number-error').html('');

应用程序/视图/电话/ SIM-未found.js.erb

$('#phone-number-found').html('');
$('#phone-number-not-found').html('Sim Number Not Found!');
$('#error').html('');

应用程序/视图/电话/ error.js.erb

$('#phone-number-found').html('');
$('#phone-number-not-found').html('');
$('#error').html('Error!');

如果有人能解决我的问题,我将不胜感激。谢谢!

0 个答案:

没有答案