单击 rails 中的提交按钮时,如何在重定向到下一页之前显示模式?
How to show a modal before redirecting on next page when clicked on a submit button in rails?
假设我有两个模型:Employer
和 Jobs
。
雇主已经为 post 一份工作创建了一个帐户,并填写了他的 phone 编号。当他填写一份新工作并单击 post 工作按钮(提交按钮)时。我想检查他输入的手机号码是否已经过验证,如果不是,我想弹出一个模式或灯箱,让他在某个号码上有未接来电,并在他打开该页面后立即弹出被重定向到工作索引页面。如果他想取消验证,他可以这样做,但页面将被重定向到任何地方,它仍然在表单页面上。
但我不知道该怎么做。请帮忙!
您可以在表单标签上触发 onsubmit
事件。
调用表单的方法 onsubmit
并从该方法中触发 ajax。检查手机号码是否通过控制器和 return true
或 false
验证。然后从 Ajax success 函数检查值(true/false),如果值为 false 显示模态和 return false else return true.
I want to check whether the mobile number that he entered is already verified or not
我建议在模型级别实现它,然后您可以通过 ajax
调用对其进行初始化:
#app/models/employer.rb
class Employer < ActiveRecord::Base
has_many :jobs
validates :phone_number, numericality: { only_integer: true }
end
#app/models/job.rb
class Job < ActiveRecord::Base
belongs_to :employer
end
我不知道你的后端......而且你似乎也在使用 Rails 3.你肯定需要考虑更新它(考虑 Rails 5 is almost out)。
这将为您提供应用程序级别的验证,这将允许您向控制器发送请求并获得 Rails 直接提供的响应:
#app/views/jobs/new.html.erb
<%= form_for @job, remote: true do |f| %>
<%= f.submit %>
<% end %>
#app/controllers/jobs_controller.rb
class JobsController < ApplicationController
respond_to :js, :json, :html
def new
@job = Job.new
@job.save
end
end
#app/assets/javascripts/application.js
$(document).on("ajax:error", "form#new_job", function(xhr, status, error) {
alert(xhr.responseText);
});
假设我有两个模型:Employer
和 Jobs
。
雇主已经为 post 一份工作创建了一个帐户,并填写了他的 phone 编号。当他填写一份新工作并单击 post 工作按钮(提交按钮)时。我想检查他输入的手机号码是否已经过验证,如果不是,我想弹出一个模式或灯箱,让他在某个号码上有未接来电,并在他打开该页面后立即弹出被重定向到工作索引页面。如果他想取消验证,他可以这样做,但页面将被重定向到任何地方,它仍然在表单页面上。
但我不知道该怎么做。请帮忙!
您可以在表单标签上触发 onsubmit
事件。
调用表单的方法 onsubmit
并从该方法中触发 ajax。检查手机号码是否通过控制器和 return true
或 false
验证。然后从 Ajax success 函数检查值(true/false),如果值为 false 显示模态和 return false else return true.
I want to check whether the mobile number that he entered is already verified or not
我建议在模型级别实现它,然后您可以通过 ajax
调用对其进行初始化:
#app/models/employer.rb
class Employer < ActiveRecord::Base
has_many :jobs
validates :phone_number, numericality: { only_integer: true }
end
#app/models/job.rb
class Job < ActiveRecord::Base
belongs_to :employer
end
我不知道你的后端......而且你似乎也在使用 Rails 3.你肯定需要考虑更新它(考虑 Rails 5 is almost out)。
这将为您提供应用程序级别的验证,这将允许您向控制器发送请求并获得 Rails 直接提供的响应:
#app/views/jobs/new.html.erb
<%= form_for @job, remote: true do |f| %>
<%= f.submit %>
<% end %>
#app/controllers/jobs_controller.rb
class JobsController < ApplicationController
respond_to :js, :json, :html
def new
@job = Job.new
@job.save
end
end
#app/assets/javascripts/application.js
$(document).on("ajax:error", "form#new_job", function(xhr, status, error) {
alert(xhr.responseText);
});