보기를 작성 또는 편집하는 데 유효성 검증 오류를 표시하는 방법을 알아낼 수 없습니다. 내 모델에서rails 3.2.8 유효성 검사 오류가보기에 표시되지 않습니다.
나는이 내 컨트롤러에서
class Person < ActiveRecord::Base
attr_accessible :first_name, :last_name
belongs_to :organization
validates_presence_of :first_name, :message => "First name can't be blank"
validates_presence_of :last_name, :message => "Last name can't be blank"
를 내보기에서
def update
@person = Person.find(params[:id])
if @person.update_attributes(params[:person])
redirect_to(:action => 'list')
else
render('edit')
end
end
을 떠날으로 브라우저에서 검증을 테스트 할 때 내가
<div>
<%= form_for(:person, :url => {:action => 'update', :id => @person.id}) do |f| %>
<%= f.errors %>
<table>
<tr>
<th>First Name:</th>
<td><%= f.text_field(:first_name) %></td>
</tr>
<tr>
<th>Last Name:</th>
<td><%= f.text_field(:last_name) %></td>
</tr>
</table>
<div>
<%= submit_tag("Update Person") %>
</div>
<% end %>
</div>
이 이름 필드가 비어 있으면 레코드를 저장하지 않고 편집하도록 리디렉션하지만 유효성 검사 오류는 표시하지 않습니다. 그러나 콘솔에서 시도해 볼 때 :
f = Person.find(1)
#returns record
f.first_name = “”
#returns “”
f.save
#returns false
f.errors
#returns @message = (:first_name => [“First name can't be blank”])
콘솔에서는 작동하지만 브라우저에서는 작동하지 않습니다. 내가 도대체 뭘 잘못하고있는 겁니까?
오류를 표시하는 방법은'<% = f.error_messages %>'또는'<% @ post.errors.full_messages.each do | msg | %>
답장을 보내 주셔서 감사합니다.하지만 f.error_messages와 "undefined method'errors '를 사용하면 #에 대해"정의되지 않은 메소드'error_messages'가 나타납니다. : NilClass "두 번째 방법을 사용할 때. –
Ria
그리고': person' 대신에'@ person'을 교환하면 –