Skip to content

Instantly share code, notes, and snippets.

@adamjgrant
Created June 13, 2013 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamjgrant/5777487 to your computer and use it in GitHub Desktop.
Save adamjgrant/5777487 to your computer and use it in GitHub Desktop.
###### routes.rb
resources :surveys do
resources :questions do
resources :responses
end
end
###### questions/new.html.erb
<%= form_for([@survey, @survey.questions.build]) do |f| %>
<% if @question.errors.any? %>
<div id="errorExplanation">
<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 :question %><br />
<%= f.text_field :question %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
###### questions_controller.rb
def create
@question = Question.new(params[:question])
@question.survey_id = Survey.find(params[:survey_id])
respond_to do |format|
if @question.save
format.html { redirect_to(@question,
:notice => 'question was successfully created.') }
format.json { render :json => @question,
:status => :created, :location => @question }
else
format.html { render :action => "new" }
format.json { render :json => @question.errors,
:status => :unprocessable_entity }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment