我的问题表有以下列:id、question、answer、created_at、updated_at、sender_id、receiver_id。
最新的问题应该出现在顶部。
问题控制器:
def index
@questions = Question.all
respond_with(@questions)
end
def show
@question = Question.find(params[:id])
respond_with(@questions)
end
答案控制器:
def new
@question = Question.find(params[:question_id])
end
def create
@question = Question.find(params[:question_id])
if @question.update_attributes(params[:question])
redirect_to questions_path
else
render :new
end
路线:
resources :questions do
resources :answers, only: [:new, :create]
end