Generated scaffolding for model via Rails

This commit is contained in:
Prelang Builder
2014-02-27 23:58:57 +00:00
committed by u7482
parent 9921d3b6d6
commit aec5ca5c47
14 changed files with 209 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 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

View File

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

View File

@@ -1,2 +1,4 @@
class Question < ActiveRecord::Base
habtm :surveys
has_many :answers
end

View File

@@ -0,0 +1,21 @@
<%= form_for(@question) do |f| %>
<% if @question.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@question.errors.count, "error") %> prohibited this question from being saved:</h2>
<ul>
<% @question.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_area :title %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

View File

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

View File

@@ -0,0 +1,27 @@
<h1>Listing questions</h1>
<table>
<thead>
<tr>
<th>Title</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% @questions.each do |question| %>
<tr>
<td><%= question.title %></td>
<td><%= link_to 'Show', question %></td>
<td><%= link_to 'Edit', edit_question_path(question) %></td>
<td><%= link_to 'Destroy', question, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Question', new_question_path %>

View File

@@ -0,0 +1,4 @@
json.array!(@questions) do |question|
json.extract! question, :id, :title
json.url question_url(question, format: :json)
end

View File

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

View File

@@ -0,0 +1,9 @@
<p id="notice"><%= notice %></p>
<p>
<strong>Title:</strong>
<%= @question.title %>
</p>
<%= link_to 'Edit', edit_question_path(@question) %> |
<%= link_to 'Back', questions_path %>

View File

@@ -0,0 +1 @@
json.extract! @question, :id, :title, :created_at, :updated_at

View File

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

View File

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

View File

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