簡體   English   中英

處理驗證錯誤消息以查看滑軌4

[英]Handle validation error messages to view rails 4

我已經實現了一種簡單的形式來在其中使用驗證碼注冊用戶。 一切工作正常,但是我無法管理要在視圖上顯示的驗證錯誤,這看起來很簡單,但是我想我在這里遺漏了一些要點。 這是我的模特

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

因此,問題是如何管理來自模型和控制器的消息,以便在單擊按鈕時顯示在視圖上。 謝謝

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