2010-07-23 4 views
10

그냥 내 모델에서 간단한 validates_presence_of를 작성했는데, 오류가 렌더링 할 때,이 호출새로운 Rails 3 앱의 템플릿이 누락 되었습니까?

Template is missing 

Missing template posts/create with {:locale=>[:en, :en], :handlers=>[:builder, :rjs, :erb, :rhtml, :rxml, :haml], :formats=>[:html]} in view paths "/Users/johnsmith/Sites/shwagr/app/views" 

오류들이 Rails3에 별도의 전망을 할 필요는 없습니다? 나는 그것이 레일즈 마술이라고 생각했다.

누군가이 문제가 있거나 그것을 올바르게 검증하는 방법을 알고 있다면 궁금하다.

내 모델 :

validates_presence_of :category, :name, :url 

내 컨트롤러 :

흥미, 내가 '응용 프로그램/조회/게시물/create.html.haml 터치'

def new 
    @post = Post.new 

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


def create 
    @post = Post.new(params[:post]) 
    if @post.valid? && current_user.posts << @post 
    respond_to do |format| 
     if @post.save 
     format.html { redirect_to(@post, :notice => 'Post was successfully created.') } 
     format.xml { render :xml => @post, :status => :created, :location => @post } 
     else 
     format.html { render :action => "new" } 
     format.xml { render :xml => @post.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 
end 

업데이트하고, 이제는 오류를 제거하고 대신 해당 페이지를로드합니다. 근데 왜? 또는 더 중요한 것은 어떻게해야 할 것처럼 new_post_path (@post)로 다시 리디렉션 할 수 있습니까?

답변

12

당신의

if @post.valid? && current_user.posts << @post 

라인이 false를 돌려주는 경우, 아니 렌더링() 또는 redirect_to()가 호출된다. Rails의 기본 동작은 메소드와 같은 이름으로 뷰를 렌더링하는 것이다. 그러면 create.BUILDER.FORMAT이됩니다.

줄을 제거하십시오.

@post = current_user.posts.new(params[:post]) 
respond_to do |format| 
    if @post.save 
    ... 

또는 아 그것을 가지고

render :action => "new" 
1

아니요, 별도의보기가 없습니다. 그럼 app/views/posts/create.html.erb 파일이 있습니까?

+0

No..form은 _form/edit/index/new/show입니다.이 형식은 다음과 같습니다. = form_for @post do | f | -if @ post.errors.any? #errorExplanation % h2 = "# {plural {@ post.errors.count,"error "}}이 게시물의 저장을 금지했습니다." % ul - @ post.errors.full_messages.each do | msg % li = msg 다른 것이 필요합니까? – Trip

+0

이상한 뭔가가 있지만 여기에 메타 링크가 생깁니다. 왜냐하면 유효성 검사는 레일스에서 ​​즉시 작동하기 때문입니다. 내 모델이 위와 같이 보이는지 업데이트했습니다. – Trip

+0

확인. 질문을 편집하여 컨트롤러 코드를 알려주십시오. –

2

과 다른 케이스를 쓰기 : 대신에이 코드를 사용합니다. 이것은 결코 유효하지 않았기 때문에 'create'에서 스스로 되돌릴 수 있었고 거기에 템플릿이없고 오류가 발생했기 때문입니다. def 생성을 설정하는 올바른 방법은 다음과 같습니다.

관련 문제