Frage

I'm using this recaptcha gem and I can't get the errors for the recaptcha to display.

My form is:

<div class="row top25">
  <%= form_for @message, url: {action: "create"}, html: {role: "form"} do |form| %>
  <fieldset class="fields">
    <div class="form-group">
      <%= form.label :name %><%= @message.errors[:name].join(", ") %>
      <%= form.text_field :name, class: "form-control"%>
    </div>

    <div class="form-group">
      <%= form.label :email %><%= @message.errors[:email].join(", ") %>
      <%= form.text_field :email, class: "form-control" %>
    </div>
    <div class="form-group">
      <%= form.label :subject %><%= @message.errors[:subject].join(", ") %>
      <%= form.select(:subject, Message::SUBJECTS,{}, class: "form-control") %>
    </div> 

    <div class="form-group">
      <%= form.label :body %><%= @message.errors[:body].join(", ") %>
      <%= form.text_area :body, class: "form-control", rows: "10" %>
    </div>
  </fieldset>
  <div class="form-group">
  <%= @message.errors.full_messages %>
  <%= recaptcha_tags :display => { :theme => "clean" }%>
  </fieldset>
</div>

<fieldset class="actions">
  <%= form.submit "Send" %>
</fieldset>
<% end %>
</div>

My controller is:

class ContactController < ApplicationController


    def new
        @message = Message.new
    end

    def create
        @message = Message.new(params[:message])


        if verify_recaptcha(model: @message, attribute: :recaptcha) && @message.save

            NotificationMailer.new_message(@message).deliver
            NotificationMailer.new_contact(@message).deliver
            redirect_to(static_pages_location_path, :notice => "Message was successfully sent.")
        else
            flash.now[:error] = @message.errors.full_messages
            render :new
        end
    end

My model is:

class Message

  SUBJECTS = ['x','y','z']

  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  attr_accessor :name, :email, :subject, :body, :recaptcha

  validates :name, :email, :subject, :body, :presence => true
  validates :subject, inclusion: SUBJECTS
  validates :email, :format => { :with => %r{.+@.+\..+} }, :allow_blank => true

  def initialize(attributes = {})
    attributes.each do |name, value|
      send("#{name}=", value)
    end
  end

  def persisted?
    false
  end

end

I've used the @message.errors.full_messages to try and display all the messages I am getting but the error I receive is only for recaptcha and not for any other input fields.

How do I display the errors for recaptcha and all other input fields?

War es hilfreich?

Lösung

In this particular case I'm not sure if it's possible to get both errors of recaptcha and ActiveRecord validator because in your conditional statement you're firstly verifying captcha validation and THEN you're trying to save.

Which means condition is true before trying to save anything. As you know, validations are performed inside save process. But in your case, you can never get any validation error unless your captcha is true.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top