<repo_name>byee322/survey_gorilla<file_sep>/public/js/application.js
$(document).ready(function() {

	$('.survey-name').on('submit', function(e){
		e.preventDefault();
		$.post("/create_survey", $("#create-survey").serialize())
		.done(function(data){
			$('.survey-name').hide();
				$('.survey-questions').html(data);

				$('button[name="add_q"]').on('click', function(e){
					e.preventDefault();
					$('.questions').append('<div class="question"><span>Question : </span><input type="text" name="survey[][title]" style="width: 70%;"><div class="answers"><input type="text" name="survey[][answers][]" placeholder="answer"> <input type="text" name="survey[][answers][]" placeholder="answer"></div><button type="submit" name="add_a">Add Answer</button></div>');
				});

				$('.questions').on('click', 'button[name="add_a"]', function(e){
					e.preventDefault();
					$(this).parent().find('.answers').append(' <input type="text" name="survey[][answers][]" placeholder="answer">');
				});

		});
	});
});
<file_sep>/db/seeds.rb
user = User.create :name => "test",
                   :email => "<EMAIL>",
                   :password => "<PASSWORD>"

survey1 = user.surveys.create :title => "Colors"

question1 = survey1.questions.create :question => "What is your favorite color?"


question1.answers.create :answer => "Red"
answer1 = question1.answers.create :answer => "Green"
question1.answers.create :answer => "Blue"
answer1.responses.create :user_id => user.id

question2 = survey1.questions.create :question => "What is your favorite drink?"
question2.answers.create :answer => "Water"
answer2 = question2.answers.create :answer => "Beer"
question2.answers.create :answer => "Wine"
answer2.responses.create :user_id => user.id, :survey_id => 1
<file_sep>/app/models/survey.rb
class Survey < ActiveRecord::Base

  validates_presence_of :title

  belongs_to :user
  has_many :questions
end
<file_sep>/app/models/answer.rb
class Answer < ActiveRecord::Base
  
  validates_presence_of :answer

  belongs_to :question
  has_many :responses
end
<file_sep>/app/controllers/index.rb
get '/' do
  erb :index
end

get '/create_survey/:id' do |id|
	@user = User.find(id)
	erb :create_survey
end

post '/create_survey' do
	@survey = Survey.create(params[:user])
  erb :make_questions, layout: false
end 

get '/make_questions/:id' do |id|
	@survey = Survey.find(id)
	erb :make_questions
end

post '/make_questions/:id' do |id|
	@survey = Survey.find(id)
	params[:survey].each do |question|
		q_title = question["title"] #q1
		ques = Question.create(question: q_title)
		@survey.questions << ques
		q_answers = question["answers"] # [a1, a2]
		q_answers.each do |answer|
			ans = Answer.create(answer: answer)
			ques.answers << ans
		end
	end
	redirect '/dashboard'
end

get '/dashboard' do
  if current_user
    @surveys = Survey.all
    @surveys_answered = @user.surveys
    erb :dashboard
  else
    redirect '/'
  end
end

get '/create_survey/:id' do |id|
  erb :create_survey
end

get '/take_survey/:id' do
    @survey = Survey.find(params[:id])  # remove this stuff and put into take_survey post
    @question_bank = @survey.questions
    @answer_bank = @survey.questions.first.answers
    erb :take_survey
end


post '/survey_submit' do 
  survey_id = params.shift.pop
  if params
    params.values.each do |answer_id|
      Response.create(user_id: session[:user_id], survey_id: survey_id,  answer_id: answer_id)
      redirect '/dashboard'
    end
  else
    redirect '/take_survey/#{:id}'
  end
end

# User.first.surveys.first.questions.first.answers.first.responses
<file_sep>/app/models/response.rb
class Response < ActiveRecord::Base

  belongs_to :answer
  belongs_to :user
  belongs_to :survey

end

<file_sep>/db/migrate/20130705113824_create_tables.rb
class CreateTables < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.text :name
      t.text :email
      t.string :password_digest
    end

    create_table :surveys do |t|
      t.integer :user_id
      t.text :title
    end

    create_table :questions do |t|
      t.integer :survey_id
      t.text :question
    end

    create_table :answers do |t|
      t.integer :question_id
      t.text :answer
    end

    create_table :responses do |t|
      t.integer :user_id
      t.integer :answer_id
      t.integer :survey_id
    end
  end

end
<file_sep>/README.md
survey_gorilla
==============

Using nested forms and dynamically adding questions and answers through ajax.

DBC Weekend challenge - Completed with a group of 3 over a weekend. 

Built form scratch a working sign-up/login survey creator. You can make your own survey and add questions and answers. You can also take surveys. 
<file_sep>/app/controllers/login.rb
before '/dashboard/*' do
  redirect '/' unless session[:user_id]
end

get '/login' do
  erb :login
end

post '/login' do
  @user = User.find_by_email(params[:login][:email])
  if @user.try(:authenticate, params[:login][:password])
    session[:user_id] = @user.id
    erb :dashboard
  else
    redirect '/login'
  end
end

post '/signup' do
  if params[:signup][:password] == params[:signup][:confirmation]
    data = {:name => params[:signup][:name],
            :email => params[:signup][:email],
            :password => params[:signup][:password]}
    @user = User.create data
    session[:user_id] = @user.id
    erb :dashboard
  else
    redirect '/login'
  end
end

get '/logout' do
  session.clear
  redirect '/'
end
<file_sep>/app/models/question.rb
class Question < ActiveRecord::Base
  
  # validates_presence_of :question

  belongs_to :survey
  has_many :answers
end
