Generated scaffolding for model via Rails

This commit is contained in:
Prelang Builder
2014-02-27 23:58:46 +00:00
committed by u7482
parent de46fe4065
commit 98e8f446a3
13 changed files with 218 additions and 0 deletions

View 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/

View 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

View File

@@ -0,0 +1,2 @@
module AnswersHelper
end

View 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 %>

View File

@@ -0,0 +1,6 @@
<h1>Editing answer</h1>
<%= render 'form' %>
<%= link_to 'Show', @answer %> |
<%= link_to 'Back', answers_path %>

View 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 %>

View 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

View File

@@ -0,0 +1,5 @@
<h1>New answer</h1>
<%= render 'form' %>
<%= link_to 'Back', answers_path %>

View 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 %>

View File

@@ -0,0 +1 @@
json.extract! @answer, :id, :response, :question_id, :created_at, :updated_at

View File

@@ -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.

View 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

View File

@@ -0,0 +1,4 @@
require 'test_helper'
class AnswersHelperTest < ActionView::TestCase
end