Skip to content

Instantly share code, notes, and snippets.

@adamjgrant
Created June 13, 2013 18:44
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/5776255 to your computer and use it in GitHub Desktop.
Save adamjgrant/5776255 to your computer and use it in GitHub Desktop.
####### config/routes.rb
match 'surveys/edit/new' => 'surveys#new'
####### ..controllers/surveys_controller.rb
class SurveysController < ApplicationController
def index
@surveys = Survey.all
respond_to do |format|
format.html # index.html.erb
format.json { render :json => @surveys }
end
end
def new
@survey = Survey.new
respond_to do |format|
format.html # new.html.erb
format.json { render :json => @survey }
end
end
def create
@survey = Survey.new(params[:survey])
respond_to do |format|
if @survey.save
format.html { redirect_to(@survey,
:notice => 'survey was successfully created.') }
format.json { render :json => @survey,
:status => :created, :location => @survey }
else
format.html { render :action => "new" }
format.json { render :json => @survey.errors,
:status => :unprocessable_entity }
end
end
end
end
####### ..views/survey/new.html.erb
<h1>New Survey</h1>
<%= form_for(@survey) do |f| %>
<% if @survey.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(@survey.errors.count, "error") %> prohibited this survey from being saved: </h2>
<ul>
<% @survey.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :question %><br />
<%= f.text_field :name %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment