Skip to content

Commit

Permalink
Added CRUD pages for the quiz model
Browse files Browse the repository at this point in the history
  • Loading branch information
sdflem committed Oct 25, 2018
1 parent ea9921f commit 7f62924
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 4 deletions.
12 changes: 10 additions & 2 deletions app/controllers/multiple_choice_questions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ def edit
end

def update
@multiple_choice_question = MultipleChoiceQuestion.find(params[:id])
begin
@multiple_choice_question = MultipleChoiceQuestion.find(params[:id])
rescue
redirect_to multiple_choice_questions_url, alert: 'Error: Multiple choice question not found'
end
if @multiple_choice_question.update(params.require(:multiple_choice_question).permit(:question, :answer, :distractor_1, :distractor_2, :distractor_3, :distractor_4))
redirect_to multiple_choice_question_url(@multiple_choice_question), notice: 'Multiple choice question was successfully updated.'
else
Expand All @@ -65,7 +69,11 @@ def update
end

def destroy
@multiple_choice_question = MultipleChoiceQuestion.find(params[:id])
begin
@multiple_choice_question = MultipleChoiceQuestion.find(params[:id])
rescue
redirect_to multiple_choice_questions_url, alert: 'Error: Multiple choice question not found'
end
@multiple_choice_question.destroy
redirect_to multiple_choice_questions_url, notice: 'Multiple choice question was successfully destroyed.'
end
Expand Down
62 changes: 62 additions & 0 deletions app/controllers/quizzes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,66 @@ def home
# render 'quizzes/home.html.erb'
end

def index
@quizzes = Quiz.all
# render 'quizzes/index.html.erb'
end

def show
begin
@quiz = Quiz.find(params[:id])
rescue
redirect_to quizzes_url, alert: 'Error: Quiz not found'
end
# render 'quizzes/show.html.erb'
end

def new
@quiz = Quiz.new
# render 'quizzes/new.html.erb'
end

def create
@quiz = Quiz.new(params.require(:quiz).permit(:title, :description))
if @quiz.save
redirect_to quiz_url(@quiz), notice: 'Quiz was successfully created.'
else
flash.now[:alert] = 'Error! Unable to create quiz.'
render :new
end
end

def edit
begin
@quiz = Quiz.find(params[:id])
rescue
redirect_to quizzes_url, alert: 'Error: Quiz not found'
end
# render 'quizzes/edit.html.erb'
end

def update
begin
@quiz = Quiz.find(params[:id])
rescue
redirect_to quizzes_url, alert: 'Error: Quiz not found'
end
if @quiz.update(params.require(:quiz).permit(:title, :description))
redirect_to quiz_url(@quiz), notice: 'Quiz was successfully updated.'
else
flash.now[:alert] = 'Error! Unable to update quiz.'
render :edit
end
end

def destroy
begin
@quiz = Quiz.find(params[:id])
rescue
redirect_to quizzes_url, alert: 'Error: Quiz not found'
end
@quiz.destroy
redirect_to quizzes_url, notice: 'Quiz was successfully destroyed.'
end

end
4 changes: 2 additions & 2 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
<li class="nav-item <%= active_class(about_path) %>">
<%= link_to 'About', about_path, class: 'nav-link' %>
</li>
<li class="nav-item <%= active_class(multiple_choice_questions_path) %>">
<%= link_to 'Teachers', multiple_choice_questions_path, class: 'nav-link' %>
<li class="nav-item <%= active_class(quizzes_path) %>">
<%= link_to 'Teachers', quizzes_path, class: 'nav-link' %>
</li>
<%
=begin
Expand Down
30 changes: 30 additions & 0 deletions app/views/quizzes/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<%= form_with model: quiz, local: true do |form| %>
<div class="field">
<%= form.label :title %>
<%= form.text_field :title, class: 'form-control' %>
<% if quiz.errors.full_messages_for(:title).any? %>
<div class="invalid-feedback">
<% quiz.errors.full_messages_for(:title).each do |error_message| %>
<%= error_message %>.
<% end %>
</div>
<% end %>
</div>

<div class="field">
<%= form.label :description %>
<%= form.text_field :description, class: 'form-control' %>
<% if quiz.errors.full_messages_for(:description).any? %>
<div class="invalid-feedback">
<% quiz.errors.full_messages_for(:description).each do |error_message| %>
<%= error_message %>.
<% end %>
</div>
<% end %>
</div>

<div class="actions">
<%= form.submit 'Submit', class: 'btn btn-primary' %>
</div>
<% end %>
13 changes: 13 additions & 0 deletions app/views/quizzes/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<% provide(:title, 'Edit Quiz') %>

<main role="main" class="container">

<h1>Edit Quiz</h1>

<%= render 'form', quiz: @quiz %>

<p>
<%= link_to 'Back', quizzes_path %>
</p>

</main><!-- /.container -->
34 changes: 34 additions & 0 deletions app/views/quizzes/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<% provide(:title, 'Quizzes') %>

<main role="main" class="container">

<h1>Quizzes</h1>

<table class="table table-sm">
<thead>
<tr>
<th scope="col">Title</th>
<th scope="col">Description</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
<% @quizzes.each do |quiz| %>
<tr>
<td><%= quiz.title %></td>
<td><%= quiz.description %></td>
<td>
<%= link_to 'Show', quiz_path(quiz) %>
<%= link_to 'Edit', edit_quiz_path(quiz) %>
<%= link_to 'Destroy', quiz_path(quiz), method: :delete %>
</td>
</tr>
<% end %>
</tbody>
</table>

<p>
<%= link_to 'New Quiz', new_quiz_path %>
</p>

</main><!-- /.container -->
13 changes: 13 additions & 0 deletions app/views/quizzes/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<% provide(:title, 'New Quiz') %>

<main role="main" class="container">

<h1>New Quiz</h1>

<%= render 'form', quiz: @quiz %>

<p>
<%= link_to 'Back', quizzes_path %>
</p>

</main><!-- /.container -->
18 changes: 18 additions & 0 deletions app/views/quizzes/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<% provide(:title, 'Show Quiz') %>

<main role="main" class="container">

<p>
<b>Title:</b> <%= @quiz.title %>
</p>

<p>
<b>Description:</b> <%= @quiz.description %>
</p>

<p>
<%= link_to 'Edit', edit_quiz_path(@quiz) %> |
<%= link_to 'Back', quizzes_path %>
</p>

</main><!-- /.container -->
8 changes: 8 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
delete 'multiple_choice_questions/:id', to: 'multiple_choice_questions#destroy'

get 'quizzes/:id/home', to: 'quizzes#home', as: 'quiz_home'
get 'quizzes', to: 'quizzes#index', as: 'quizzes'
post 'quizzes', to: 'quizzes#create'
get 'quizzes/new', to: 'quizzes#new', as: 'new_quiz'
get 'quizzes/:id/edit', to: 'quizzes#edit', as: 'edit_quiz'
get 'quizzes/:id', to: 'quizzes#show', as: 'quiz'
put 'quizzes/:id', to: 'quizzes#update'
patch 'quizzes/:id', to: 'quizzes#update'
delete 'quizzes/:id', to: 'quizzes#destroy'

# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

0 comments on commit 7f62924

Please sign in to comment.