diff --git a/app/assets/javascripts/questions.js.coffee b/app/assets/javascripts/questions.js.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/questions.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/questions_controller.rb b/app/controllers/questions_controller.rb new file mode 100644 index 0000000..ccf9fb0 --- /dev/null +++ b/app/controllers/questions_controller.rb @@ -0,0 +1,74 @@ +class QuestionsController < ApplicationController + before_action :set_question, only: [:show, :edit, :update, :destroy] + + # GET /questions + # GET /questions.json + def index + @questions = Question.all + end + + # GET /questions/1 + # GET /questions/1.json + def show + end + + # GET /questions/new + def new + @question = Question.new + end + + # GET /questions/1/edit + def edit + end + + # POST /questions + # POST /questions.json + def create + @question = Question.new(question_params) + + respond_to do |format| + if @question.save + format.html { redirect_to @question, notice: 'Question was successfully created.' } + format.json { render action: 'show', status: :created, location: @question } + else + format.html { render action: 'new' } + format.json { render json: @question.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /questions/1 + # PATCH/PUT /questions/1.json + def update + respond_to do |format| + if @question.update(question_params) + format.html { redirect_to @question, notice: 'Question was successfully updated.' } + format.json { head :no_content } + else + format.html { render action: 'edit' } + format.json { render json: @question.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /questions/1 + # DELETE /questions/1.json + def destroy + @question.destroy + respond_to do |format| + format.html { redirect_to questions_url } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_question + @question = Question.find(params[:id]) + end + + # Never trust parameters from the scary internet, only allow the white list through. + def question_params + params.require(:question).permit(:title) + end +end diff --git a/app/helpers/questions_helper.rb b/app/helpers/questions_helper.rb new file mode 100644 index 0000000..2eaab4a --- /dev/null +++ b/app/helpers/questions_helper.rb @@ -0,0 +1,2 @@ +module QuestionsHelper +end diff --git a/app/models/question.rb b/app/models/question.rb index 0ba7683..740c149 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -1,2 +1,4 @@ class Question < ActiveRecord::Base + habtm :surveys + has_many :answers end diff --git a/app/views/questions/_form.html.erb b/app/views/questions/_form.html.erb new file mode 100644 index 0000000..2491d8f --- /dev/null +++ b/app/views/questions/_form.html.erb @@ -0,0 +1,21 @@ +<%= form_for(@question) do |f| %> + <% if @question.errors.any? %> +
+

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

+ + +
+ <% end %> + +
+ <%= f.label :title %>
+ <%= f.text_area :title %> +
+
+ <%= f.submit %> +
+<% end %> diff --git a/app/views/questions/edit.html.erb b/app/views/questions/edit.html.erb new file mode 100644 index 0000000..5a1b530 --- /dev/null +++ b/app/views/questions/edit.html.erb @@ -0,0 +1,6 @@ +

Editing question

+ +<%= render 'form' %> + +<%= link_to 'Show', @question %> | +<%= link_to 'Back', questions_path %> diff --git a/app/views/questions/index.html.erb b/app/views/questions/index.html.erb new file mode 100644 index 0000000..63d13b9 --- /dev/null +++ b/app/views/questions/index.html.erb @@ -0,0 +1,27 @@ +

Listing questions

+ + + + + + + + + + + + + <% @questions.each do |question| %> + + + + + + + <% end %> + +
Title
<%= question.title %><%= link_to 'Show', question %><%= link_to 'Edit', edit_question_path(question) %><%= link_to 'Destroy', question, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New Question', new_question_path %> diff --git a/app/views/questions/index.json.jbuilder b/app/views/questions/index.json.jbuilder new file mode 100644 index 0000000..4275734 --- /dev/null +++ b/app/views/questions/index.json.jbuilder @@ -0,0 +1,4 @@ +json.array!(@questions) do |question| + json.extract! question, :id, :title + json.url question_url(question, format: :json) +end diff --git a/app/views/questions/new.html.erb b/app/views/questions/new.html.erb new file mode 100644 index 0000000..8b78e7c --- /dev/null +++ b/app/views/questions/new.html.erb @@ -0,0 +1,5 @@ +

New question

+ +<%= render 'form' %> + +<%= link_to 'Back', questions_path %> diff --git a/app/views/questions/show.html.erb b/app/views/questions/show.html.erb new file mode 100644 index 0000000..a6a1363 --- /dev/null +++ b/app/views/questions/show.html.erb @@ -0,0 +1,9 @@ +

<%= notice %>

+ +

+ Title: + <%= @question.title %> +

+ +<%= link_to 'Edit', edit_question_path(@question) %> | +<%= link_to 'Back', questions_path %> diff --git a/app/views/questions/show.json.jbuilder b/app/views/questions/show.json.jbuilder new file mode 100644 index 0000000..4aa4527 --- /dev/null +++ b/app/views/questions/show.json.jbuilder @@ -0,0 +1 @@ +json.extract! @question, :id, :title, :created_at, :updated_at diff --git a/config/routes.rb b/config/routes.rb index 4ddc38b..7d2d106 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ PrelangTest::Application.routes.draw do + resources :questions + resources :answers devise_for :users, controllers: {registrations: "users/registrations", sessions: "users/sessions", passwords: "users/passwords", omniauth_callbacks: "users/omniauth_callbacks"}, skip: [:sessions, :registrations] diff --git a/test/controllers/questions_controller_test.rb b/test/controllers/questions_controller_test.rb new file mode 100644 index 0000000..354ebe5 --- /dev/null +++ b/test/controllers/questions_controller_test.rb @@ -0,0 +1,49 @@ +require 'test_helper' + +class QuestionsControllerTest < ActionController::TestCase + setup do + @question = questions(:one) + end + + test "should get index" do + get :index + assert_response :success + assert_not_nil assigns(:questions) + end + + test "should get new" do + get :new + assert_response :success + end + + test "should create question" do + assert_difference('Question.count') do + post :create, question: { title: @question.title } + end + + assert_redirected_to question_path(assigns(:question)) + end + + test "should show question" do + get :show, id: @question + assert_response :success + end + + test "should get edit" do + get :edit, id: @question + assert_response :success + end + + test "should update question" do + patch :update, id: @question, question: { title: @question.title } + assert_redirected_to question_path(assigns(:question)) + end + + test "should destroy question" do + assert_difference('Question.count', -1) do + delete :destroy, id: @question + end + + assert_redirected_to questions_path + end +end diff --git a/test/helpers/questions_helper_test.rb b/test/helpers/questions_helper_test.rb new file mode 100644 index 0000000..a190288 --- /dev/null +++ b/test/helpers/questions_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class QuestionsHelperTest < ActionView::TestCase +end