Ruby on rails 处理验证错误消息以查看rails 4
我已经实现了一个简单的表单,用它来注册带有captcha的用户。 一切正常,但我无法管理要在视图上显示的验证错误,这看起来很简单,但我想我遗漏了一些要点。 这是我的模型Ruby on rails 处理验证错误消息以查看rails 4,ruby-on-rails,validation,ruby-on-rails-4,view,error-handling,Ruby On Rails,Validation,Ruby On Rails 4,View,Error Handling,我已经实现了一个简单的表单,用它来注册带有captcha的用户。 一切正常,但我无法管理要在视图上显示的验证错误,这看起来很简单,但我想我遗漏了一些要点。 这是我的模型 class Email < ActiveRecord::Base apply_simple_captcha :message => "The secret Image and code were different", :add_to_base => true validates_format_
class Email < ActiveRecord::Base
apply_simple_captcha :message => "The secret Image and code were different", :add_to_base => true
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/ , :message => "Invalid Email Format"
end
那么这就是我的观点
<%= form_for(@email, url: emails_new_path) do |f| %>
<div class="field">
<%= f.text_field :email, placeholder: "Enter Email" %>
</div>
<%= show_simple_captcha(:email=>"email", :label => "Human Authentication", :placeholder => "Enter the code") %>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<%= @email.errors.full_messages.first if @email.errors.any? %>
<% end %>
控制器
class EmailsController < ApplicationController
def new
@email = Email.new(user_params)
if !user_params.nil?
if simple_captcha_valid?
puts "Right captcha"
@email.save!
if @email.save
flash[:success] = "Thanks! We will be in touch soon!"
redirect_to :action => 'new'
else
flash[:success] = "Invalid Emails"
render :action => 'new'
end
else
puts "Wrong captcha"
flash[:success] = "Wrong Emails"
end
end
end
def user_params
params.require(:email).permit(:email) if params[:email]
end
end
所以问题是如何管理来自模型和控制器的消息,以便在单击按钮时显示在视图上。
谢谢您可以使用带验证码的保存进行验证,如:
@email=email.newuser\u参数
如果@email.save\u带有验证码
正确的验证码
否则
使用验证码设置错误
结束