maxiperez
maxiperez

Reputation: 1480

Do not show error messages when validate a form

When the form is validated, this must show the error message. However it´s not showing.

My view looks like this:

<% form_for :invite, :url => profile_invites_path do |f| -%>
<%= f.error_messages %>
<p><label for="email_to"><%= t 'invites.new.labels.mail'%></label><br/>
<%= f.text_field :email_to %></p>

<p><label for="role_id"><%= t 'invites.new.labels.role'%></label><br/>
<%= collection_select(:invite, :type, Role.players , :name, :printable_name) %></p>

<p><label for="message"><%= t 'invites.new.labels.message'%></label><br/>
<%= f.text_area :message %></p>

<p><%= submit_tag "#{ t 'application.send'}" %></p>
<% end -%>

My model looks like this

class Invite < ActiveRecord::Base
  self.inheritance_column = "invite_type"

  RE_EMAIL_NAME   = '[\w\.%\+\-]+'                          # what you actually see in practice
  #RE_EMAIL_NAME   = '0-9A-Z!#\$%\&\'\*\+_/=\?^\-`\{|\}~\.' # technically allowed by RFC-2822
  RE_DOMAIN_HEAD  = '(?:[A-Z0-9\-]+\.)+'
  RE_DOMAIN_TLD   = '(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|jobs|museum)'
  RE_EMAIL_OK     = /\A#{RE_EMAIL_NAME}@#{RE_DOMAIN_HEAD}#{RE_DOMAIN_TLD}\z/i
  MSG_EMAIL_BAD   = "should look like an email address."

  # Validates
  validate :validate_presence_mail, :message => :"email.blank"
  validates_format_of :email_to, :on => :update, :with => RE_EMAIL_OK, :message => MSG_EMAIL_BAD
  validates_uniqueness_of :email_to, :on => :update, :scope => :profile_id, :message => :"email_to.taken"
  validates_presence_of :token
  validates_uniqueness_of :token
  validates_length_of :message, :minimum => 5

  def validate_presence_mail
    presence_of(self.email_to, "email.to")
  end

  def presence_of(attrib, field)
    if attrib.blank?
      error_path = I18n.t "activerecord.errors.full_messages.#{field}.blank"
      self.errors.add_to_base("#{error_path}")
    end
  end
end

Although error messages syntax is specified in the view and errors are added in the model, they are not being shown.


This is the form in the view

<% content_for :header do -%>
<% t 'invites.new.title'%> <%= configatron.site_name %>
<% end -%>

<% content_for :sidebar do -%>

<p>
    <% t 'invites.new.indication'%>
</p>
<% end -%>



<% form_for @invite, :url => profile_invites_path do |f| -%>
<%= f.error_messages %>
<p><label for="email_to"><%= t 'invites.new.labels.mail'%></label><br/>
<%= f.text_field :email_to %></p>

<p><label for="role_id"><%= t 'invites.new.labels.role'%></label><br/>
<%= collection_select(:invite, :type, Role.players , :name, :printable_name) %></p>

<p><label for="message"><%= t 'invites.new.labels.message'%></label><br/>
<%= f.text_area :message %></p>

<p><%= submit_tag "#{ t 'application.send'}" %></p>
<% end -%>

<%= link_to "#{ t 'application.back'}", profile_invites_path %>

This is the new controller

def new
    @invite = Invite.new
  end

  def index
    @profile = current_user.profile
    @ps_invites = Invite.of_profile(@profile.id).for_powerful_supplier.available.count
    @ubc_invites = Invite.of_profile(@profile.id).for_business_contact.available.count

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @invites }
    end
  end

  def create
    @invite = Invite.of_type(params[:invite][:type]).of_profile(current_user.profile.id).available.first

    unless @invite.nil?
      @invite.status = 'pending'
      @invite.message = params[:invite][:message]
      @invite.email_to = params[:invite][:email_to]
      if @invite.save
        InviteMailer.deliver_send_invite(@invite)
        flash.now[:notice] = t 'invites.messages.sent'
        redirect_to(profile_invites_url)
      else
        flash.now[:error] =  t "invites.messages.not_sent"
        render :controller => 'invites' ,:action => "new"
      end
    else
      flash.now[:error] = t("invites.messages.without_invite", :profile_type => params[:invite][:type])
      render :controller => 'invites' ,:action => "new"
    end
  end
end

Upvotes: 0

Views: 621

Answers (1)

mark
mark

Reputation: 10564

Should work with this:

<% form_for @invite, :url => profile_invites_path do |f| -%>

Upvotes: 1

Related Questions