验证表单时,必须显示错误消息。但它没有显示。
我的观点如下:
<% 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 -%>
我的模型看起来像这样
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
虽然在视图中指定了错误消息语法,并且在模型中添加了错误,但它们未显示。
这是视图中的表单
<% 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 %>
这是新控制器
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
答案 0 :(得分:1)
应该用这个:
<% form_for @invite, :url => profile_invites_path do |f| -%>