gpt4 book ai didi

javascript - 在 Rails 中使用相同模型验证两种表单的参数

转载 作者:行者123 更新时间:2023-12-03 04:54:53 25 4
gpt4 key购买 nike

所以我最近实现了一个电话/SIM 卡检查器。第一个表单输入要求用户输入他们的电话号码。如果数据库中存在电话号码,则呈现电话号码找到消息,否则将电话号码形式替换为 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

应用程序/ Controller /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

app/views/phones/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 %>

app/views/phones/phone-found.js.erb

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

app/views/phones/phone-not-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();

app/views/phones/error.js.erb

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

app/views/phones/_errors.html.erb

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

app/views/phones/_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 %>

app/views/phones/sim-found.js.erb

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

app/views/phones/sim-not-found.js.erb

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

app/views/phones/sim-error.js.erb

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

app/views/phones/_sim-errors.html.erb

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

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

最佳答案

您可以使用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

关于javascript - 在 Rails 中使用相同模型验证两种表单的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42474753/

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