2012-11-24 3 views
0

보기를 작성 또는 편집하는 데 유효성 검증 오류를 표시하는 방법을 알아낼 수 없습니다. 내 모델에서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”]) 

콘솔에서는 작동하지만 브라우저에서는 작동하지 않습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

+1

오류를 표시하는 방법은'<% = f.error_messages %>'또는'<% @ post.errors.full_messages.each do | msg | %>

  • <%= msg %>
  • <% end %>' –

    +0

    답장을 보내 주셔서 감사합니다.하지만 f.error_messages와 "undefined method'errors '를 사용하면 # 에 대해"정의되지 않은 메소드'error_messages'가 나타납니다. : NilClass "두 번째 방법을 사용할 때. – Ria

    +0

    그리고': person' 대신에'@ person'을 교환하면 –

    답변

    0

    오류 메시지를 표시하는 데 잘못된 방법을 사용하고 있습니다. <%= f.error_messages %>이고 <%= f.errors %>이 아님. 이 링크를보세요 rails_rendering_errors

    0

    lynda.com과 K. Skoglund의 도움으로 오류를 수정하는 방법을 알았습니다. 우선 오류 메시지

    <% if object && object.errors.size > 0 %> 
        <div> 
         <h2><%= pluralize(object.errors.size, "error") %> 
         prohibited this record from being saved</h2> 
         <p>There were problems with the following fields:</p> 
         <ul> 
         <% object.errors.full_messages.each do |msg| %> 
          <li><%= msg %></li> 
         <% end %> 
         </ul> 
         </div> 
        <% end %> 
    

    을 처리 할 부분은 그럼 난 형태

    def error_messages_for (object) 
        render(:partial => 'shared/error_messages', 
        :locals => {:object => object}) 
    end 
    

    마지막에 호출하는 도우미 메서드를 생성 생성, 그 부분 폼의 상단에 도우미 메서드를 추가 내가 편집하고 새로운 템플릿을 모두 사용하기 위해 만든 : 그 직접 귀하의 질문에 대답하지 않지만

    <%= error_messages_for(@person) %> 
    <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> 
    
    0

    은 내가보기 엔 코드의 종류에 '레일 애자일 개발'을 작성 중지하는 것이 좋습니다. excelent 보석 등을 잘 받기 :

    • decent_exposure
    • simple_form
    • HAML 레일

    간단한 일을 할 수 상용구 코드의 톤을 기록 중지합니다.

    관련 문제