diff --git a/app/assets/javascripts/answers.js.coffee b/app/assets/javascripts/answers.js.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/answers.js.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/controllers/answers_controller.rb b/app/controllers/answers_controller.rb new file mode 100644 index 0000000..d9340a4 --- /dev/null +++ b/app/controllers/answers_controller.rb @@ -0,0 +1,74 @@ +class AnswersController < ApplicationController + before_action :set_answer, only: [:show, :edit, :update, :destroy] + + # GET /answers + # GET /answers.json + def index + @answers = Answer.all + end + + # GET /answers/1 + # GET /answers/1.json + def show + end + + # GET /answers/new + def new + @answer = Answer.new + end + + # GET /answers/1/edit + def edit + end + + # POST /answers + # POST /answers.json + def create + @answer = Answer.new(answer_params) + + respond_to do |format| + if @answer.save + format.html { redirect_to @answer, notice: 'Answer was successfully created.' } + format.json { render action: 'show', status: :created, location: @answer } + else + format.html { render action: 'new' } + format.json { render json: @answer.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /answers/1 + # PATCH/PUT /answers/1.json + 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 + + # DELETE /answers/1 + # DELETE /answers/1.json + def destroy + @answer.destroy + respond_to do |format| + format.html { redirect_to answers_url } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_answer + @answer = Answer.find(params[:id]) + end + + # Never trust parameters from the scary internet, only allow the white list through. + def answer_params + params.require(:answer).permit(:response, :question_id) + end +end diff --git a/app/helpers/answers_helper.rb b/app/helpers/answers_helper.rb new file mode 100644 index 0000000..b7cdb29 --- /dev/null +++ b/app/helpers/answers_helper.rb @@ -0,0 +1,2 @@ +module AnswersHelper +end diff --git a/app/views/answers/_form.html.erb b/app/views/answers/_form.html.erb new file mode 100644 index 0000000..aec3f19 --- /dev/null +++ b/app/views/answers/_form.html.erb @@ -0,0 +1,25 @@ +<%= form_for(@answer) do |f| %> + <% if @answer.errors.any? %> +
+

<%= pluralize(@answer.errors.count, "error") %> prohibited this answer from being saved:

+ + +
+ <% end %> + +
+ <%= f.label :response %>
+ <%= f.text_area :response %> +
+
+ <%= f.label :question_id %>
+ <%= f.text_field :question_id %> +
+
+ <%= f.submit %> +
+<% end %> diff --git a/app/views/answers/edit.html.erb b/app/views/answers/edit.html.erb new file mode 100644 index 0000000..92f7d3f --- /dev/null +++ b/app/views/answers/edit.html.erb @@ -0,0 +1,6 @@ +

Editing answer

+ +<%= render 'form' %> + +<%= link_to 'Show', @answer %> | +<%= link_to 'Back', answers_path %> diff --git a/app/views/answers/index.html.erb b/app/views/answers/index.html.erb new file mode 100644 index 0000000..c91d17d --- /dev/null +++ b/app/views/answers/index.html.erb @@ -0,0 +1,29 @@ +

Listing answers

+ + + + + + + + + + + + + + <% @answers.each do |answer| %> + + + + + + + + <% end %> + +
ResponseQuestion
<%= answer.response %><%= answer.question %><%= link_to 'Show', answer %><%= link_to 'Edit', edit_answer_path(answer) %><%= link_to 'Destroy', answer, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New Answer', new_answer_path %> diff --git a/app/views/answers/index.json.jbuilder b/app/views/answers/index.json.jbuilder new file mode 100644 index 0000000..383a314 --- /dev/null +++ b/app/views/answers/index.json.jbuilder @@ -0,0 +1,4 @@ +json.array!(@answers) do |answer| + json.extract! answer, :id, :response, :question_id + json.url answer_url(answer, format: :json) +end diff --git a/app/views/answers/new.html.erb b/app/views/answers/new.html.erb new file mode 100644 index 0000000..20757c4 --- /dev/null +++ b/app/views/answers/new.html.erb @@ -0,0 +1,5 @@ +

New answer

+ +<%= render 'form' %> + +<%= link_to 'Back', answers_path %> diff --git a/app/views/answers/show.html.erb b/app/views/answers/show.html.erb new file mode 100644 index 0000000..5b46534 --- /dev/null +++ b/app/views/answers/show.html.erb @@ -0,0 +1,14 @@ +

<%= notice %>

+ +

+ Response: + <%= @answer.response %> +

+ +

+ Question: + <%= @answer.question %> +

+ +<%= link_to 'Edit', edit_answer_path(@answer) %> | +<%= link_to 'Back', answers_path %> diff --git a/app/views/answers/show.json.jbuilder b/app/views/answers/show.json.jbuilder new file mode 100644 index 0000000..1295268 --- /dev/null +++ b/app/views/answers/show.json.jbuilder @@ -0,0 +1 @@ +json.extract! @answer, :id, :response, :question_id, :created_at, :updated_at diff --git a/config/routes.rb b/config/routes.rb index d9bbbe6..4ddc38b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ PrelangTest::Application.routes.draw do + resources :answers + devise_for :users, controllers: {registrations: "users/registrations", sessions: "users/sessions", passwords: "users/passwords", omniauth_callbacks: "users/omniauth_callbacks"}, skip: [:sessions, :registrations] get "landings/index" # The priority is based upon order of creation: first created -> highest priority. diff --git a/test/controllers/answers_controller_test.rb b/test/controllers/answers_controller_test.rb new file mode 100644 index 0000000..984ebd1 --- /dev/null +++ b/test/controllers/answers_controller_test.rb @@ -0,0 +1,49 @@ +require 'test_helper' + +class AnswersControllerTest < ActionController::TestCase + setup do + @answer = answers(:one) + end + + test "should get index" do + get :index + assert_response :success + assert_not_nil assigns(:answers) + end + + test "should get new" do + get :new + assert_response :success + end + + test "should create answer" do + assert_difference('Answer.count') do + post :create, answer: { question_id: @answer.question_id, response: @answer.response } + end + + assert_redirected_to answer_path(assigns(:answer)) + end + + test "should show answer" do + get :show, id: @answer + assert_response :success + end + + test "should get edit" do + get :edit, id: @answer + assert_response :success + end + + test "should update answer" do + patch :update, id: @answer, answer: { question_id: @answer.question_id, response: @answer.response } + assert_redirected_to answer_path(assigns(:answer)) + end + + test "should destroy answer" do + assert_difference('Answer.count', -1) do + delete :destroy, id: @answer + end + + assert_redirected_to answers_path + end +end diff --git a/test/helpers/answers_helper_test.rb b/test/helpers/answers_helper_test.rb new file mode 100644 index 0000000..a044015 --- /dev/null +++ b/test/helpers/answers_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class AnswersHelperTest < ActionView::TestCase +end