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 %>
index action
,then you must uncomment the codeindex action
in thecontroller
. – Pavan