0
votes

My error says that it has: "undefined method `errors' for nil:NilClass" and "NoMethodError in Questions#index"

Where the error says the error is.

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

My questions controller where i think the error is.

    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

My full _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>

App/Views/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 %>
1
If your file belongs to index action,then you must uncomment the code index action in the controller.Pavan

1 Answers

1
votes

Your file app/views/questions/edit.html.erb and app/views/questions/new.html.erb are most probably calling some partial... i would guess the partial is:

app/views/questions/_form.html.erb

Is that right?

In any case, that partial probably has these two lines at the top...

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

So you are calling the _error_messages partial without specifying what the object in the partial should reference. So change it to...

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

Alternatively, you can do...

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

Which does exactly the same thing but is slightly nicer because the form needs to mention the instance variable only once.