2014-07-26 3 views
0

내 오류는 가지고 있다고 말한다 : 오류가 오류가 말한다와 "NoMethodError 질문에 # 지수"정의되지 않은 방법 오류 전무는 "오류": 클래스

"NilClass 정의되지 않은 메서드`전무에 대한 오류 '" .

<% if object.errors.any? %> 
<ul id="form-errors"> 
    <% object.errors.full_messages.each do |message| %> 
     <li><%= message %></li> 

내 질문 컨트롤러 어디에서 오류가 발생했는지 생각해보십시오.

class QuestionsController < ApplicationController 
    before_filter :auth, only: [:create, :your_questions, :edit, :update] 

    # def index 
    #  @question = Question.new 
    # @questions = Question.unsolved(params) 
    # end 

    def self.unsolved(params) 
    order('created_at DESC').where(solved: false).paginate(page: params[:page],per_page: 3) 
    end 

    def create 
    @question = current_user.questions.build(params[:question]) 
    if @question.save 
     flash[:success] = 'Your question has been posted!' 
     redirect_to @question 
    else 
     @questions = Question.unsolved(params) 
     render 'index' 
    end 
    end 

    def new 
     @question = Question.new 
    end 

    def show 
    puts params 
    @question = Question.find(params[:id]) 
    @answer = Answer.new 
    end 

    def your_questions 
    @questions = current_user.your_questions(params[:id]) 
    end 

    def edit 
    @question = current_user.questions.find(params[:id]) 
    end 

    def update 
    @question = current_user.questions.find(params[:id]) 

    if @question.update_attributes(params[:question]) 
     flash[:success] = 'Your question has been updated!' 
     redirect_to @question 
    else 
     render 'edit' 
    end 
    end 

    def search 
    @questions = Question.search(params) 
    end 
end 

내 전체 _question_form.html.erb

<%= form_for(@question) do |f| %> 
<%= render 'shared/error_messages', object: f.object %> 

<p> 
    <%= f.label :body, "Question" %><br /> 
    <%= f.text_field :body %> 

    <%= f.submit "Ask a Question" %> 
</p> 

앱/조회/new.html.erb

<% provide(:title, 'Make It Snappy Q&A - Register') %> 

<h1>Register</h1> 

<%= form_for(@user) do |f| %> 
    <%= render 'common/form_errors', object: @user %> 

    <p> 
     <%= f.label :username %><br /> 
     <%= f.text_field :username %> 
    </p> 

    <p> 
     <%= f.label :password %><br /> 
     <%= f.password_field :password %> 
    </p> 

    <p> 
     <%= f.label :password_confirmation, 'Confirm' %><br /> 
     <%= f.password_field :password_confirmation %> 
    </p> 

    <p> 
     <%= f.submit "Register" %> 
    </p> 
<% end %> 
+0

파일이'index action'에 속한다면'controller'에서'index action' 코드의 주석 처리를 제거해야합니다. – Pavan

답변

1

파일 app/views/questions/edit.html.erbapp/views/questions/new.html.erb 대부분의 아마 부분을 요구하고있다 ... 부분은 다음과 같을 것입니다.

app/views/questions/_form.html.erb

맞습니까? 부분은 아마 상단의 두 라인을 가지고있는 경우

... 그래서이 부분에 object가 참조해야하는지 지정하지 않고 일부이든 _error_messages를 호출

<%= form_for(@question) do |f| %> 
    <%= render 'shared/error_messages' %> 

. 그래서 ... 당신이 할 수있는, 또는 ... 정확히 같은 일을하지만 형태는 한 번만 인스턴스 변수를 언급 할 필요가 있기 때문에 약간 좋네요

<%= form_for(@question) do |f| %> 
    <%= render 'shared/error_messages', object: f.object %> 

<%= form_for(@question) do |f| %> 
    <%= render 'shared/error_messages', object: @question %> 

을 그것에 변경 .

+0

코드 대신에 다음과 같은 오류 메시지가 표시됩니다. "정의되지 않은 메서드 오류 'for'nil : NilClass"하지만 네가 내 부분적인 부분에 대해 닫습니다. _question_form.html.erb –

+0

확인해주세요. (귀하의 질문을 개정하여) 전체'_question_form.html.erb' 감사합니다! – SteveTurczyn

+0

정말 고마워! 한 단계 더 ...'app/views/new.html.erb'와'app/views/edit.html.erb'에 대한 코드를 보여주세요. – SteveTurczyn