Created
October 22, 2013 17:32
-
-
Save EminenceHC/7104725 to your computer and use it in GitHub Desktop.
.dup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% title "Take Survey" %> | |
<%= form_for @survey, :url => { :action => "clone"}, method: :post do |f| %> | |
<p> | |
<%= f.label :name %><br /> | |
<%= f.text_field :name %> | |
<%= f.hidden_field :student_id %> | |
</p> | |
<%= f.fields_for :questions do |builder| %> | |
<%= render "question_fields", :f => builder %> | |
<% end %> | |
<p><%= f.submit "Submit" %></p> | |
<% end %> | |
<p> | |
<%= link_to "Show", @survey %> | | |
<%= link_to "View All", surveys_path %> | |
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Eminence::Application.routes.draw do | |
get "counselors/index" | |
root :to => "home#index" | |
#devise_scope :user do | |
# root to: "devise/sessions#new" | |
#end | |
devise_for :users, :controllers => {:registrations => "registrations"} | |
resources :users | |
resources :students | |
resources :counselors | |
resources :drug_assessments | |
get 'surveys/:id/clone_edit', to: 'surveys#clone_edit', as: :clone_edit_survey | |
post 'surveys/:id/clone', to: 'surveys#clone', as: :clone_survey | |
post 'search' => 'students#search' | |
resources :surveys | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SurveysController < ApplicationController | |
def index | |
@surveys = Survey.all | |
end | |
def show | |
@survey = Survey.find(params[:id]) | |
end | |
def new | |
@survey = Survey.new | |
3.times do | |
question = @survey.questions.build | |
4.times { question.answers.build } | |
end | |
end | |
def create | |
@survey = Survey.new(survey_params) | |
if @survey.save | |
flash[:notice] = "Successfully created survey." | |
redirect_to @survey | |
else | |
render :action => 'new' | |
end | |
end | |
def edit | |
@survey = Survey.find(params[:id]) | |
end | |
def clone_edit | |
@survey = Survey.find(params[:id]) | |
end | |
def clone | |
@survey = Survey.find params[:id] | |
@clone = @survey.dup | |
@clone.student_id = current_user.loginable.id | |
if @clone.save | |
redirect_to surveys_path | |
flash[:notice] = "Successfully cloned survey." | |
else | |
render json: { error: "Clone failed", messages: @clone.errors.full_messages } | |
end | |
end | |
def update | |
@survey = Survey.find(params[:id]) | |
if @survey.update_attributes(survey_params) | |
flash[:notice] = "Successfully updated survey." | |
redirect_to @survey | |
else | |
render :action => 'edit' | |
end | |
end | |
def destroy | |
@survey = Survey.find(params[:id]) | |
@survey.destroy | |
flash[:notice] = "Successfully destroyed survey." | |
redirect_to surveys_url | |
end | |
private | |
def survey_params | |
params.require(:survey).permit! | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment