<file_sep>source 'https://rubygems.org'

gem 'rails', '4.0.1'
gem 'pg', '0.17.1'

# Json builder
gem 'jbuilder', '~> 1.2'

# JavaScript
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails', '3.1.0'
gem 'jquery-ui-rails', '4.1.1'
gem 'angularjs-rails', '1.2.14'

# CSS
gem 'sass-rails', '~> 4.0.0'
gem 'compass-rails', '~> 1.1.3'
gem 'bootstrap-sass', '~> 3.2.0.0'
gem 'uglifier', '~> 2.5.3'

# Icons FontAwesome
gem 'font-awesome-rails', '~> 4.1.0.0'

# Slim
gem 'slim', :git => 'git://github.com/brennancheung/slim.git', :branch => 'angularjs_support'
gem 'slim-rails', '2.1.0'
<file_sep>namespace :db do
  desc 'add categories'
  task add_categories: :environment do
    unless Api::Category.find_by_name('Category 1')
      category = Api::Category.new(
                  name: 'Category 1',
                  count: 0
                )
      category.save
      puts '---> added category Category 1'
    end
    
    unless Api::Category.find_by_name('Category 2')
      category = Api::Category.new(
                  name: 'Category 2',
                  count: 0
                )
      category.save
      puts '---> added category Category 2'
    end

    unless Api::Category.find_by_name('Category 3')
      category = Api::Category.new(
                  name: 'Category 3',
                  count: 0
                )
      category.save
      puts '---> added category Category 3'
    end
  end
end
<file_sep>json.array!(@api_questions) do |api_question|
  json.extract! api_question, :id, :status, :title, :body, :comments_count, :category_id
end
<file_sep>json.extract! @api_category, :id, :name, :count, :created_at, :updated_at
<file_sep>class Api::CommentsController < ApplicationController
  before_action :set_api_comment, only: [:show, :edit, :update, :destroy]

  def index
    @api_comments = Api::Comment.where(question_id: params[:question_id])
  end

  def create
    @api_comment = Api::Comment.new(api_comment_params)

    if @api_comment.save
      render json: { comment: @api_comment }, status: :created
    else
      render json: @api_comment.errors, status: :unprocessable_entity
    end
  end

  def update
    if @api_comment.update(api_comment_params)
      respond_to json { head :no_content }
    else
      respond_to json { render json: @api_comment.errors, status: :unprocessable_entity }
    end
  end

  def destroy
    @api_comment.destroy
    respond_to json { head :no_content }
  end

  private
    def set_api_comment
      @api_comment = Api::Comment.find(params[:id])
    end

    def api_comment_params
      params.require(:comment).permit(:body, :question_id)
    end
end
<file_sep>namespace :db do
  desc 'add questions'
  task add_questions: :environment do

    category = Api::Category.first
    puts "---> take first category '#{category.name}'"
  
    # Create question for first category
    quest = Api::Question.where(title: "Question 1")
    unless quest.any?
      question = Api::Question.new(
                  category_id: category.id,
                  status: 'open',
                  title: 'Question 1',
                  body: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolorem, repudiandae.',
                  comments_count: 0
                )
      question.save
      puts "---> added question for #{category.name}"
    end

    quest = Api::Question.where(title: "Question 2")
    unless quest.any?
      question = Api::Question.new(
                  category_id: category.id,
                  status: 'ballot',
                  title: 'Question 2',
                  body: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolorem, repudiandae.',
                  comments_count: 0
                )
      question.save
      puts "---> added question for #{category.name}"
    end

    quest = Api::Question.where(title: "Question 3")
    unless quest.any?
      question = Api::Question.new(
                  category_id: category.id,
                  status: 'close',
                  title: 'Question 3',
                  body: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolorem, repudiandae.',
                  comments_count: 0
                )
      question.save
      puts "---> added question for #{category.name}"
    end
  end
end
<file_sep>namespace :db do  
  desc 'delete custom added data'
  task delete_data: :environment do
    Api::Category.delete_all
    Api::Question.delete_all
    Api::Comment.delete_all
    puts '---> deleted all data'
  end
end
<file_sep>class Api::QuestionsController < ApplicationController
  before_action :set_api_question, only: [:show, :edit, :update, :status, :destroy]

  def index
    @api_questions = Api::Question.where(category_id: params[:category_id])
  end

  def create
    @api_question = Api::Question.new(api_question_params)

    if @api_question.save
      respond_to json { render action: 'show', status: :created, location: @api_question }
    else
      respond_to json { render json: @api_question.errors, status: :unprocessable_entity }
    end
  end

  def update
    if @api_question.update(api_question_params)
      respond_to json { head :no_content }
    else
      respond_to json { render json: @api_question.errors, status: :unprocessable_entity }
    end
  end

  def status
    if @api_question.update(status_params)
      render json: { status: @api_question.status }, status: :created
    else
      render json: @api_question.errors, status: :unprocessable_entity
    end
  end

  def destroy
    @api_question.destroy
    respond_to json { head :no_content }
  end

  private
    def set_api_question
      @api_question = Api::Question.find(params[:id])
    end

    def api_question_params
      params.require(:question).permit(:status, :title, :body, :comments_count, :category_id)
    end

    def status_params
      params.permit(:status)
    end
end
<file_sep>Question &amp; Answer
===============

"Question &amp; Answer" with Ruby on Rails 4, AngularJS, PostgreSQL.

For populate the database run next two rake tasks:
```
rake db:add_categories
rake db:add_questions
```
For delete all fakes data from database run next task:
```
rake db:delete_data
```
<file_sep>RorAngularQuestions::Application.routes.draw do
  namespace :api do
    resources :categories do
      resources :questions do
        member do
          put :status
        end

        resources :comments
      end
    end
  end

  root 'main#index'
  match '*path' => 'main#index', via: [:get, :post]
end
<file_sep>json.array!(@api_comments) do |api_comment|
  json.extract! api_comment, :id, :body, :question_id
end
<file_sep>class Api::CategoriesController < ApplicationController
  before_action :set_api_category, only: [:show, :edit, :update, :destroy]

  def index
    @api_categories = Api::Category.all.order("id ASC")
  end

  def create
    @api_category = Api::Category.new(api_category_params)

    if @api_category.save
      respond_to json { render action: 'show', status: :created, location: @api_category }
    else
      respond_to json { render json: @api_category.errors, status: :unprocessable_entity }
    end
  end

  def update
    if @api_category.update(api_category_params)
      respond_to json { head :no_content }
    else
      respond_to json { render json: @api_category.errors, status: :unprocessable_entity }
    end
  end

  def destroy
    @api_category.destroy
    respond_to json { head :no_content }
  end

  private
    def set_api_category
      @api_category = Api::Category.find(params[:id])
    end

    def api_category_params
      params.require(:api_category).permit(:name, :count)
    end
end
<file_sep>json.array!(@api_categories) do |api_category|
  json.extract! api_category, :id, :name, :count
end
<file_sep>json.extract! @api_comment, :id, :body, :question_id, :created_at, :updated_at
<file_sep># register .slim for assets pipeline
Rails.application.assets.register_mime_type 'text/html', '.html'
Rails.application.assets.register_engine '.slim', Slim::Template