在两个表单中使用相同的模型验证params

时间:2017-02-26 21:59:58

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

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

我目前遇到的问题是验证每个表单输入的最小和最大长度。当我输出电话号码表单的错误时,我得到:Sim号码太短(最少12个字符),Sim号码太长(最多12个字符)。填写后一个SIM号码表时,电话号码太短(最少11个字符),电话号码太长(最多12个字符)。

如何让每个表单只针对其参数进行验证,而不是其他形式?

以下代码: 应用程序/模型/ 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 = Phone.new(phone_params)
      if @phone.invalid?
        render 'error'
      else
        @phone = Phone.where(phone_number: params[:phone][:phone_number])
        respond_to do |format|
          if @phone.exists?
            format.js {render 'phone-found'}
          else
            format.js {render 'phone-not-found'}
          end
        end
      end
  end

  def checksim
    @sim = Phone.new(sim_params)
      if @sim.invalid?
        render 'sim-error'
      else
        @sim = Phone.where('sim_number = ?', params[:sim][:sim_number])
        respond_to do |format|
          if @sim.exists?
            format.js {render 'sim-found'}
          else @sim.blank?
            format.js {render 'sim-not-found'}
          end
        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, minlength: 11, maxlength: 11, required: true %>
  <%= 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();

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

$('#phone-number-found').html('');
$('#phone-number-not-found').html('');
$('#error').html('<%= j render('errors') %>');

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

<% @phone.errors.full_messages.each do |message| %>
    <p>- <%= message %></p>
<% end %>

应用程序/视图/电话/ _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, minlength: 12, maxlength: 12, required: true %>
  <%= 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('');

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

$('#phone-number-found').html('');
$('#phone-number-not-found').html('');
$('#error').html('<%= j render('sim_errors') %>');

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

<% @sim.errors.full_messages.each do |message| %>
    <p>- <%= message %></p>
<% end %>

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

1 个答案:

答案 0 :(得分:1)

您可以使用allow_nil: true。如果未使用表单提交项目,则会忽略该项目。

validates :phone_number, length: {minimum: 11, maximum: 11}, allow_nil: true
validates :sim_number, length: {minimum: 12, maximum: 12}, allow_nil: true