2013-08-08 17 views
17

Obecnie uczę siebie trochę RoR i robię samouczek, ale dodając trochę ładniejszego układu i rzeczy z bootstrapem i mam problem, którego nie mogę rozwiązać."Nieokreślona metoda" błędy "dla zer: NilClass" podczas wywoływania metody błędów

Próbuję zrobić część walidacji (http://guides.rubyonrails.org/getting_started.html#adding-some-validation), ale gdy używam:

<% @post.errors.any? %> 

otrzymuję komunikat:

undefined method `errors' for nil:NilClass 
Extracted source (around line #9): 
<legend><h1>Add Post</h1></legend> 

<%= form_for :post, url: posts_path, html: {class: 'form-horizontal'} do |f| %> 
     <% if @post.errors.any? %> 
     <div id="errorExplanation"> 

nic nie działa, a ja nawet kopiować i wklejać części z samouczka.

Oto kod dla widoku:

<p> </p> 

<div class="span6" 

<fieldset> 
    <legend><h1>Add Post</h1></legend> 

    <%= form_for :post, url: posts_path, html: {class: 'form-horizontal'} do |f| %> 
      <% if @post.errors.any? %> 
      <div id="errorExplanation"> 

       <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2> 

       <ul> 
        <% @post.errors.full_messages.each do |msg| %> 
        <li><%= msg %></li> 
        <% end %> 
        </ul> 
      </div> 
    <% end %> 
     <div class="control-group"> 
      <%= f.label :title, :class => 'control-label' %> 
      <div class="controls"> 
       <%= f.text_field :title, :class => 'span4' %> 
      </div> 
     </div> 

     <div class="control-group"> 
      <%= f.label :content, :class => 'control-label' %> 
      <div class="controls"> 
       <%= f.text_area :content, :rows => '7', :class => 'input-block-level' %> 
      </div> 
     </div> 

     <div class="form-actions"> 
      <%= f.submit "Add Post", :class => 'btn btn-success' %> 
      <%= link_to "Cancel", posts_path, :class => 'btn', :style => 'float:right;' %> 
     </div> 
    <% end %> 
</fieldset> 

</div> 

A moja posts_controller:

class PostsController < ApplicationController 

    def new 
    end 

    def create 
     @post = Post.new(params[:post].permit(:title, :content)) 

     if @post.save 
      redirect_to @post 
     else 
      render 'new' 
     end 
    end 

    def show 
     @post = Post.find(params[:id]) 
    end 

    def index 
     @posts = Post.order("created_at desc") 
    end 

    private 
     def post_params 
      params.require(:post).permit(:title, :content) 
     end 

end 

Czego mi brakuje? Z góry dziękuję!

Odpowiedz

51

Musisz również zdefiniować @post w swojej akcji new.

def new 
    @post = Post.new 
end 

Dostaniesz błąd NilClass ponieważ @post ma wartość (to nil) gdy najpierw załadować formularz na działanie new.

Po wykonaniu render :new w swoim działaniu create nie ma problemu, ponieważ jest za pomocą @post już zdefiniowany na górze create.

+0

@Deefour Cześć. Robię ten sam samouczek i otrzymuję ten sam błąd w "5.12 Aktualizuję posty". Zmienna '@ post' jest zdefiniowana w nowej metodzie i ciągle wyrzuca ten błąd. Czy możesz mi pomóc? – Demnogonis

+0

Miałem ten sam problem. To naprawdę zagmatwane, ponieważ górna część samouczka nie wspomina wyraźnie, że trzeba napisać . Nowość w akcji Nowa. – DennyHiu

1

Zaktualizuj metodę create w pliku posts.controller.rb za pomocą poniższego fragmentu kodu. To zadziałało dla mnie.

def create 
    @post = Post.new(params[:post].permit(:title, :text)) 
    @post.save 
    redirect_to @post 
end 
-2
def new 
    @article = Article.new 
end 
Powiązane problemy