언어 모델, 질문 모델 및 응답 모델이있는 응용 프로그램을 작성하고 있습니다. 나는 다음과 같은 범주로 하나 "내 질문"인덱스 뷰 페이지에 대한 질문의 목록과 답변을 표시 할 것 : 하나의보기에서 데이터를 분류하는 방법
- 내 대답없는 질문은 (현재 사용자에 속하는 경우에만 응답)을 의미 현재 사용자에 속하는 경우에만 질문) 나는 답변
- 질문 (의미
내가 프로그래밍에 새로운 오전 잘 모르겠어요 경우 단어를 연구 "카테고리"에서 Rails는 이에 대한 올바른 용어였습니다. t이 방법으로 도움이되는 정보를 찾을 수 없었습니다. 어떤 통찰력이라도 대단히 감사 할 것입니다! 여기
내 경로입니다 : 여기class Question < ActiveRecord::Base
require 'obscenity/active_model'
has_many :answers, dependent: :destroy
accepts_nested_attributes_for :answers, :reject_if => :all_blank, :allow_destroy=>true
belongs_to :user
belongs_to :language
default_scope { order('created_at DESC') }
validates :body, obscenity: true
validates :body, length: { minimum: 10, maximum: 160 }, presence: true
validates :user, presence: true
end
내 대답 모델입니다 : 여기 Languagecheck::Application.routes.draw do
devise_for :users
resources :users
resources :languages do
resources :questions, except: [:index] do
resources :answers
end
end
resources :questions, only: [:index, :new, :create]
get 'about' => 'welcome#about'
root to: 'welcome#index'
end
내 질문 모델입니다
class Answer < ActiveRecord::Base
require 'obscenity/active_model'
belongs_to :question
belongs_to :user
validates :name, obscenity: true
validates :body, length: { minimum: 1, maximum: 160 }, presence: true
end
내 사용자 모델을 설정 곳 사용자는 많은 질문과 답변을 가지고 있습니다.
다음class QuestionsController < ApplicationController
before_action :set_language, except: [:index]
before_action :set_question, only: [:show, :edit, :update, :destroy]
def index
@questions = current_user.questions
end
def show
@question = Question.find(params[:id])
@answer = Answer.new
end
def new
@question = Question.new
authorize @question
end
def edit
@question = Question.find(params[:id])
authorize @question
end
def update
@question = Question.find(params[:id])
authorize @question
if @question.update_attributes(question_params)
flash[:notice] = "Question was updated."
redirect_to [@language, @question]
else
flash[:error] = "There was an error saving the question. Please try again."
render :edit
end
end
def create
@question = Question.new(question_params)
@question.language = @language
@question.user = current_user
authorize @question
if @question.save
flash[:notice] = "Question was saved."
redirect_to [@language, @question]
else
flash[:error] = "There was an error saving the question. Please try again."
render :new
end
end
def destroy
@question.destroy.find(params[:id])
respond_to do |format|
format.html { redirect_to questions_url }
format.json { head :no_content }
end
end
private
def set_question
@question = Question.find(params[:id])
end
def question_params
params.require(:question).permit(:id, :body)
end
def set_language
@language = Language.find(params[:language_id])
end
end
내 대답 컨트롤러 :
class AnswersController < ApplicationController
before_action :set_language_and_question
before_action :set_answer, only: [:show, :edit, :update, :destroy]
def index
@answers = Answer.all
end
def show
end
def new
@answer = Answer.new
end
def edit
end
def create
@answer = Answer.new(answer_params)
@answer.question = @question
@answer.user = current_user
respond_to do |format|
if @answer.save
format.html { redirect_to [@language, @question], notice: 'Answer was successfully created.' }
format.json { render action: 'show', status: :created, location: @answer }
else
format.html { redirect_to [@language, @question], error: 'Answer was not successfully created.' }
format.json { render json: @answer.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @answer.update(answer_params)
format.html { redirect_to @answer, notice: 'Answer was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @answer.errors, status: :unprocessable_entity }
end
end
end
def destroy
@answer.destroy
respond_to do |format|
format.html { redirect_to answers_url }
format.json { head :no_content }
end
end
private
def set_answer
@answer = Answer.find(params[:id])
@answer ||= @question.answers.new
end
def answer_params
params.require(:answer).permit(:id, :body)
end
def set_language_and_question
@language = Language.find(params[:language_id])
@question = Question.find(params[:question_id])
end
end
그리고 여기에 "내 질문"보기 (이 단지 순간에 모든 질문을 나열한다)는 다음
내 질문 컨트롤러 :<div class="row">
<div class="col-md-8">
<h3>My Unanswered Questions</h3>
<% @questions.each do |question| %>
<div class="media">
<div class="media-body">
<h4 class="media-heading">
<p><%= question.language ? link_to(question.body, [question.language, question]) : question.body %></p>
</h4>
<small>
submitted <%= time_ago_in_words(question.created_at) %> ago by <%= question.user.name %> <br/>
<%= question.answers.count %> Answers
</small>
</div>
</div>
<% end %>
</div>
<br/>
<br/>
<div class="col-md-4">
<%= link_to "Ask a New Question", new_question_path, class: 'btn btn-success' %>
</div>
</div>
<div class="row">
<div class="col-md-8">
<h3>My Answered Questions</h3>
<% @questions.each do |question| %>
<div class="media">
<div class="media-body">
<h4 class="media-heading">
<p><%= question.language ? link_to(question.body, [question.language, question]) : question.body %></p>
</h4>
<small>
submitted <%= time_ago_in_words(question.created_at) %> ago by <%= question.user.name %> <br/>
<%= question.answers.count %> Answers
</small>
</div>
</div>
<% end %>
</div>
</div>
<div class="row">
<div class="col-md-8">
<h3>Questions I Answered</h3>
<% @questions.each do |question| %>
<div class="media">
<div class="media-body">
<h4 class="media-heading">
<p><%= question.language ? link_to(question.body, [question.language, question]) : question.body %></p>
</h4>
<small>
submitted <%= time_ago_in_words(question.created_at) %> ago by <%= question.user.name %> <br/>
<%= question.answers.count %> Answers
</small>
</div>
</div>
<% end %>
</div>
</div>
카테고리별로 언급 한 내용이 100 % 명확하지는 않지만 질문 모델에 3 가지 범위 (3 가지 사례 각각에 하나씩)를 추가하고 3 가지를 채우기 위해 충분합니다. 당신의'QuestionsController # index'에 인스턴스 변수를 넣고 템플릿에있는 것들을 사용합니까? – Matt
그들은 대답하지 않은/대답하는/대답 한 3 가지 카테고리로 그룹을 말하고 있습니다. 나는 두 가지 하위 유형의 질문 (대답/대답 안함)을 갖고 있고 두 번째 유형은 대답이라고 생각하고 있습니다. 그 중 하나 또는 두 가지 유형 : 질문/답변, 당신이 group_by on을 사용하는 응답 부울을 가진 질문. –