Generated scaffolding for model via Rails
This commit is contained in:
3
app/assets/javascripts/answers.js.coffee
Normal file
3
app/assets/javascripts/answers.js.coffee
Normal file
@@ -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/
|
||||
74
app/controllers/answers_controller.rb
Normal file
74
app/controllers/answers_controller.rb
Normal file
@@ -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
|
||||
2
app/helpers/answers_helper.rb
Normal file
2
app/helpers/answers_helper.rb
Normal file
@@ -0,0 +1,2 @@
|
||||
module AnswersHelper
|
||||
end
|
||||
25
app/views/answers/_form.html.erb
Normal file
25
app/views/answers/_form.html.erb
Normal file
@@ -0,0 +1,25 @@
|
||||
<%= form_for(@answer) do |f| %>
|
||||
<% if @answer.errors.any? %>
|
||||
<div id="error_explanation">
|
||||
<h2><%= pluralize(@answer.errors.count, "error") %> prohibited this answer from being saved:</h2>
|
||||
|
||||
<ul>
|
||||
<% @answer.errors.full_messages.each do |msg| %>
|
||||
<li><%= msg %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div class="field">
|
||||
<%= f.label :response %><br>
|
||||
<%= f.text_area :response %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= f.label :question_id %><br>
|
||||
<%= f.text_field :question_id %>
|
||||
</div>
|
||||
<div class="actions">
|
||||
<%= f.submit %>
|
||||
</div>
|
||||
<% end %>
|
||||
6
app/views/answers/edit.html.erb
Normal file
6
app/views/answers/edit.html.erb
Normal file
@@ -0,0 +1,6 @@
|
||||
<h1>Editing answer</h1>
|
||||
|
||||
<%= render 'form' %>
|
||||
|
||||
<%= link_to 'Show', @answer %> |
|
||||
<%= link_to 'Back', answers_path %>
|
||||
29
app/views/answers/index.html.erb
Normal file
29
app/views/answers/index.html.erb
Normal file
@@ -0,0 +1,29 @@
|
||||
<h1>Listing answers</h1>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Response</th>
|
||||
<th>Question</th>
|
||||
<th></th>
|
||||
<th></th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
<% @answers.each do |answer| %>
|
||||
<tr>
|
||||
<td><%= answer.response %></td>
|
||||
<td><%= answer.question %></td>
|
||||
<td><%= link_to 'Show', answer %></td>
|
||||
<td><%= link_to 'Edit', edit_answer_path(answer) %></td>
|
||||
<td><%= link_to 'Destroy', answer, method: :delete, data: { confirm: 'Are you sure?' } %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<br>
|
||||
|
||||
<%= link_to 'New Answer', new_answer_path %>
|
||||
4
app/views/answers/index.json.jbuilder
Normal file
4
app/views/answers/index.json.jbuilder
Normal file
@@ -0,0 +1,4 @@
|
||||
json.array!(@answers) do |answer|
|
||||
json.extract! answer, :id, :response, :question_id
|
||||
json.url answer_url(answer, format: :json)
|
||||
end
|
||||
5
app/views/answers/new.html.erb
Normal file
5
app/views/answers/new.html.erb
Normal file
@@ -0,0 +1,5 @@
|
||||
<h1>New answer</h1>
|
||||
|
||||
<%= render 'form' %>
|
||||
|
||||
<%= link_to 'Back', answers_path %>
|
||||
14
app/views/answers/show.html.erb
Normal file
14
app/views/answers/show.html.erb
Normal file
@@ -0,0 +1,14 @@
|
||||
<p id="notice"><%= notice %></p>
|
||||
|
||||
<p>
|
||||
<strong>Response:</strong>
|
||||
<%= @answer.response %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<strong>Question:</strong>
|
||||
<%= @answer.question %>
|
||||
</p>
|
||||
|
||||
<%= link_to 'Edit', edit_answer_path(@answer) %> |
|
||||
<%= link_to 'Back', answers_path %>
|
||||
1
app/views/answers/show.json.jbuilder
Normal file
1
app/views/answers/show.json.jbuilder
Normal file
@@ -0,0 +1 @@
|
||||
json.extract! @answer, :id, :response, :question_id, :created_at, :updated_at
|
||||
@@ -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.
|
||||
|
||||
49
test/controllers/answers_controller_test.rb
Normal file
49
test/controllers/answers_controller_test.rb
Normal file
@@ -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
|
||||
4
test/helpers/answers_helper_test.rb
Normal file
4
test/helpers/answers_helper_test.rb
Normal file
@@ -0,0 +1,4 @@
|
||||
require 'test_helper'
|
||||
|
||||
class AnswersHelperTest < ActionView::TestCase
|
||||
end
|
||||
Reference in New Issue
Block a user