我想讓我的用戶模型驗證錯誤與其相應的表單域內聯顯示。我正在使用bootstrap和設計。我有客戶端驗證,所以它不會讓你提交註冊表單,直到大多數驗證滿足。問題是當用戶通過omniauth註冊Facebook時。 Omniauth將採用Facebook提供給我的字段,並將這些字段用於用戶,但Facebook未提供的字段將在頁面頂部引發難看的錯誤。我一直在試圖找到一種方法讓這些內聯顯示。如何顯示內聯驗證錯誤而不是頁面頂部
這是我的用戶型號:
class User < ActiveRecord::Base
has_many :bills, :dependent => :destroy
validates :cell_phone, presence: true
validates :cell_phone, length: { is: 10 }
validates :cell_phone, :numericality => {:only_integer => true}
validates :first_name, presence: true
validates :last_name, presence: true
validates :terms, presence: true
geocoded_by :last_sign_in_ip
after_validation :geocode
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :omniauthable,
:recoverable, :rememberable, :trackable, :validatable
def ability
@ability ||= Ability.new(self)
end
delegate :can?, :cannot?, :to => :ability
def self.from_omniauth(auth)
where(auth.slice(:sprovider, :uid)).first_or_create do |user|
user.sprovider = auth.provider
user.uid = auth.uid
user.first_name = auth.info.first_name
user.last_name = auth.info.last_name
user.email = auth.info.email
user.cell_phone = auth.info.cell_phone
user.avatar = auth.info.image
end
end
這是我想內嵌錯誤出現在色器件註冊表格:
<div class="row sign_in">
<div class="col-md-6 col-md-offset-3 col-lg-4 col-lg-offset-4">
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="row">
<div class="form-group">
<div class="col-md-6">
<%= f.text_field :first_name, autofocus: true , class:'form-control',placeholder:'First Name *' , :required => true %>
</div>
<div class="col-md-6">
<%= f.text_field :last_name, class:'form-control',placeholder:'Last Name*' , :required => true %>
</div>
<div class="row">
</div>
</div>
</div>
<div class="form-group">
<%= f.phone_field :cell_phone, class:'form-control',placeholder:'Mobile Number*(Solely for notification purposes)', :required => true, :maximum => 10, :minimum => 10 %>
</div>
<div class="form-group">
<%= f.email_field :email, class:'form-control',placeholder:'Email*', :required => true %>
</div>
<% if f.object.password_required? %>
<div class="form-group">
<% if @validatable %><i></i><% end %>
<%= f.password_field :password, autocomplete: "off", class:'form-control',placeholder:'Password* (8 characters minimum)', id:"myPassword", :required => true %>
</div>
<div class="form-group">
<%= f.password_field :password_confirmation, autocomplete: "off", class:'form-control',placeholder:'Password Confirmation*', :required => true %>
</div>
<% end %>
<div class="form-group">
<%= f.check_box :terms%>
I have read and agreed the User
<a href="#" data-toggle="modal" data-target=".bs-example-modal-sm">Terms and Conditions</a>
<div class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
<div class="modal-dialog modal-md">
<div class="modal-content">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec pretium libero eget posuere ornare. Donec rutrum augue eu erat fringilla posuere. Aenean tempus dui a erat facilisis posuere. Pellentesque eget nulla et turpis mollis egestas. Donec molestie consequat ultricies. Vivamus in feugiat risus. Ut a quam id sapien fringilla efficitur eget in justo. Suspendisse tincidunt libero metus. Sed ultrices auctor diam mattis pretium. Integer in aliquam dui. Praesent pulvinar ante non urna consectetur, eget porta lorem porta. Etiam ac orci pretium, faucibus neque vel, egestas velit. Nullam condimentum ultrices imperdiet. Maecenas vel nisi commodo, bibendum purus dapibus, porttitor felis.
Nullam nec porta lectus. Vivamus porta lobortis justo, sed cursus arcu tincidunt et. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Pellentesque a rhoncus tellus. Ut mollis orci quis lacus consequat, quis varius nunc auctor. Ut elit ligula, bibendum quis mauris a, ornare elementum dui. Phasellus commodo ipsum eu augue consequat porta.</p>
</div>
</div>
</div>
</div>
<div><i class="icon-heart"></i></div>
<%= f.submit 'Sign up', class:'btn btn-primary btn-block' %>
<% end %>
</div>
</div>
</body>
</html>
這與Facebook的按鈕符號:
<div><i class="icon-heart"></i></div>
<%= f.submit 'Sign in', class:'btn btn-primary btn-block' %>
<%= link_to "Sign in with facebook", user_omniauth_authorize_path(:facebook), class:'btn btn-primary btn-block' %>
<br>
<div align="center">
</div>
任何有關如何去顯示這些驗證錯誤的幫助,非常感謝。由於
不知道我得到你問的東西,但建議只是因爲你要求顯示輸入元素附近的特定字段錯誤。 – 2014-09-20 23:00:57