简体   繁体   English

处理验证错误消息以查看滑轨4

[英]Handle validation error messages to view rails 4

I have implemented simple form to register users with captcha in it. 我已经实现了一种简单的形式来在其中使用验证码注册用户。 Everything is working fine, but I could not able to manage validation errors to be shown on views, it looks easy but I guess I am missing some points here. 一切工作正常,但是我无法管理要在视图上显示的验证错误,这看起来很简单,但是我想我在这里遗漏了一些要点。 here is my model 这是我的模特

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

Then this my view 那我的看法

<%= 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 %>

Controller 控制者

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

So the question is how can I manage messages both from model and controller to be shown on view on button click. 因此,问题是如何管理来自模型和控制器的消息,以便在单击按钮时显示在视图上。 Thanks 谢谢

您可以使用save_with_captcha进行验证,例如:

@email = Email.new(user_params)

if @email.save_with_captcha

puts "Right captcha"

else

puts "error with captcha"

end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM