CSC 486: CodeWorkout – Q&A Forum Version Zero

TL: DR

Sorry for the late post. In recent news, we have completed Version Zero. As I said in my previous post, I worked on the show and index views for responses and questions, along with the editing their controllers. But, I didn’t explain what the other were doing and how the fits in the major project.

Vincent

He had the job of creating the delete and destroy features for the questions. This includes making views and controllers surround those features. To see Vincent’s progress see here blog for more details.

Jacob

His job was to make the create and edit features for questions. This includes making views and controllers for the features. To see Jacob’s progress see here blog for more details.

Austin

He had the job of edit and add features for responses. This includes making views and controllers for the features. To see Austin’s progress see here blog for more details.


The Result

After making everything we came together and combined are repositories branches into Jacob’s master repository. This merge came with quite a few issues, at first, ranging bad or that’s fine. Now, after that learning experience, we all have a better understanding of the process involved with emerging. I will write a post going into detail in the process at a later time.

Below is what we completed after we merged all are repositories branches . The next steps will take us to Version 1 which I will blog about shortly in my next post. Here is a link to the Repo that we all actively working on.

Views

  • Question#Show
%h1
	=@question.title
%tr
	- if @question.user_id != nil
		%td 
			=@question.user_id
	%td= link_to "Edit", edit_question_path(@question)
	%td= link_to "Delete", @question, method: :delete 	
%p 	
	=@question.body
%p= "Posted: #{@question.created_at}" 
- if @question.tags != nil	
	%p
		=@question.tags
- if @responses.size == 0
	%h3 No Responses
- else
	%h3 Responses:
	%table
		%tr
			%th Text:
			%th User:
			%th Posted
	- @responses.each do |response|
		%tr
			%td= link_to response.text, response
			%td= response.user_id
			%td= response.created_at
  • Question#New
=link_to questions_path do
    .btn.btn-default
        %span.glyphicon.glyphicon-arrow-left
            Back

%h1 Create a new Question

=render(:partial => 'application/record_error_messages', :locals => {:record => @question})

= form_for(@question, url: questions_path) do |f|

    =render partial: 'form', :locals => {:f => f}
    = submit_tag("Submit")
  • Question#Update
%h1 Questions#update
%p Find me in app/views/questions/update.html.haml
= params
%br/

= @question.inspect
  • Question#Index
%h1 List of Questions
-if @questions.size == 0
	%em = No Questions Found
-else
	%table
		%r
			%th Title: 
			%th User:
			%th Posted:
		- @questions.each do |question|
			%tr
				%td= link_to question.title, question
				%td= question.user_id
				%td= question.created_at
				%td= link_to "Edit", edit_question_path(question)
				%td= link_to "Delete", question, method: :delete
  • Question#Edit
=link_to questions_path do
    .btn.btn-default
        %span.glyphicon.glyphicon-arrow-left
            Back

%h1 Edit Question
%h3
    =@question.title

=render(:partial => 'application/record_error_messages', :locals => {:record => @question})

= form_for(@question, url: question_path(@question.id)) do |f|

    =render partial: 'form', :locals => {:f => f}
    = submit_tag("Submit")
  • Response#Edit
%h1 Edit Response

=render(:partial => 'application/record_error_messages', :locals => {:record => @response})

= form_for(@response, url: response_path) do |f|
  = f.label(:text, "Edit your response:")
  %br/
  = f.text_area(:text, value: @response.text)
  = submit_tag("Submit")
  • Response#Show
%tr
	%td= link_to "Edit", edit_response_path(@response)
	%td
		= form_for @response, url: response_path(@response.id), html: {:onsubmit => "return confirm('Are you sure you want to delete the question?');"}, method: :delete do |f|
			= submit_tag('Delete')
%p= @response.text
%p= @response.user_id
%p= (@response.created_at).strftime("%m-%d-%Y")

Controllers

  • Questions
class QuestionsController < ApplicationController
  def index
     #pre:
	#Question.all: Sellects all items in the table Question
     #post: All items of the Question table are shown
	#question#index is rendered
	@questions = Question.all
  end

  def show
     #pre:
	#exercise_id: The exercise this question should be associated with
	#pasrams[:id] (optional): The exercise this resqonses should be associated with
     #post: The sellected item of the Question table is shown along with any optional items sellected from the Response table
	#question#show is rendered
	@question = Question.find(params[:id])
	@responses = Response.all.where(question_id: params[:id])
  end

  def show
    @question = Question.find(params[:id])
    @responses = Response.all.where(question_id: params[:id])
    @response = Response.new
  end

  def new
    #pre:
      #exercise_id (optional): the exercise this question should be associated
      #with
    #post: a new question object is instantiated but not saved
      #question#new is rendered

    @question = Question.new({
      :exercise_id => params[:exercise_id]
      })

  end

  def create
    #pre:
      #params: the parameters to be used to create this question
    #post: 
      #a new question object is saved -> redirect to index
      #OR new question is not saved -> render new
    @question = Question.new(safe_assign)
    @question.user_id = current_user.id

    if @question.save
      flash[:success] = "Question saved!"
      redirect_to questions_path
    else
      flash[:error] = "Error creating your question."
      render 'new'
    end

  end

  def edit
    #pre:
      #id: the id of the question to edit
    #post:
      #edit view is rendered
    @question = Question.find(params[:id])
  end

  def update
    #pre:
      #params: question attributes to be assigned
    #post
      #the question object is saved -> redirect to index
      #OR new question is not saved -> render edit
    @question = Question.find(params[:id])
    @question.assign_attributes(safe_assign)

    if @question.save
      flash[:success] = "Question saved!"
      redirect_to questions_path
    else
      flash[:error] = "Error updating question."
      render 'edit'      
    end
  end

  def delete
    @question = Question.find(params[:question_id])
  end

  def destroy
    @question = Question.find(params[:id])
    @question.destroy
    redirect_to questions_path
    flash[:success] = "You have successfully deleted the question"    
  end

  private
  def safe_assign
    params.require(:question).permit(:title, :body, :tags, :exercise_id)
  end

end
  • Responses
class ResponsesController < ApplicationController
  def index
  end

  def show
	@response = Response.find(params[:id])
  end

  def new
  end

  def create
    @response = Response.new(safe_assign)
    @response.user_id = current_user.id
    @question = Question.find(@response.question_id)
    @responses = Response.all.where(question_id: @question.id) 

    if @response.save
      flash[:success] = "Response saved!"
      redirect_to question_path(@response.question.id)
    else
      flash[:error] = "Error creating your response."
      render 'questions/show'
    end
  end

  def edit
    @response = Response.find(params[:id])
  end

  def update
    @response = Response.find(params[:id])
    @response.assign_attributes(safe_assign)

    if @response.save
      flash[:success] = "Response edit saved!"
      redirect_to question_path(@response.question.id)
    else
      flash[:error] = "Error editing your response."
      render :template => 'responses/edit', :id => @response
    end
  end

  def delete
    @response = Response.find(params[:response_id])
  end

  def destroy
    @response = Response.find(params[:id])
    @response.destroy
    redirect_to questions_path(@response.question_id)
    flash[:success] = "You have successfully deleted the response"    
  end

  private
  def safe_assign
    params.require(:response).permit( :text, :question_id )
  end

end
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s