Skip to content

Instantly share code, notes, and snippets.

@benolee
Created June 1, 2012 05:23
Show Gist options
  • Select an option

  • Save benolee/2849084 to your computer and use it in GitHub Desktop.

Select an option

Save benolee/2849084 to your computer and use it in GitHub Desktop.
routing for nested resources

SurveyApp

This should give you all the REST routes for surveys and survey_sections, namespaced under admin, as well as the path and url helpers admin_sections_path, etc. Typing all this on my phone so just running this example might not work so good.

Check out the Rails Guide for routing, especially the nested resources section: http://guides.rubyonrails.org/routing.html#nested-resources

omg howto

$ git clone git://gist.github.com/<gist-id>.git survey_app
$ cd survey_app
$ bundle --path vendor/bundle --binstubs .bundle/bin
$ RAILS_ENV=production .bundle/bin/rackup -p 3000 -s thin

# new shell
$ curl -s http://0.0.0.0:3000/admin/surveys/new
$ curl -s -X POST -d 'survey[whatnot]=whatchathinkinbout' http://0.0.0.0:3000/admin/surveys
$ curl -s -X GET http://0.0.0.0:3000/admin/surveys/1
class ActiveRecord::Base
establish_connection \
adapter: 'sqlite',
database: ':memory'
create_table :users do |t|
t.string :email
t.string :sekrit_password
end
create_table :surveys do |t|
t.string :whatnot
t.integer :user_id
t.timestamps
end
create_table :survey_sections do |t|
t.string :magic_data
t.integer :survey_id
t.timestamps
end
end
class User < ActiveRecord::Base
has_many :surveys
has_many :survey_sections, through: :surveys
# seed
User.create \
email: '[email protected]',
sekrit_password: 'boobies'
end
class Survey < ActiveRecord::Base
belongs_to :user
has_many :survey_sections
end
class SurveySection < ActiveRecord::Base
belongs_to :survey
end
SurveyApp::Application.routes.draw do
root to: "admin/surveys#new"
namespace :admin do
resources :surveys do
resources :survey_sections
end
end
end
require 'bundler'
Bundler.require
require 'rails'
require 'rails/all'
app_dir = ::File.expand_path('..',__FILE__)
$LOAD_PATH.unshift app_dir
# config/application.rb
module SurveyApp
class Application < Rails::Application
   config.secret_token = "49837489qkuweoiuoqwehisuakshdjksadhaisdy78o34y138974xyqp9rmye8yrpiokeuioqwzyoiuxftoyqiuxrhm3iou1hrzmjk"
config.cache_classes = true
   config.middleware.delete "Rack::Lock"
   config.middleware.delete "ActionDispatch::Flash"
   config.middleware.delete "ActionDispatch::BestStandardsSupport"
end
end
require 'config-routes'
require 'app-models'
module Admin
class SurveysController < ActionController::Base
# HAX
def current_user; User.first; end
def new
@survey = Survey.new
render text: "omg #{@survey.inspect} is a new Survey!"
end
def create
@survey = Survey.create params[:survey]
redirect_to admin_surveys_path(@survey)
end
def show
@survey = Survey.find params[:id]
render text: "the survey u just made: #{survey}"
end
# too lazy for index, update, destroy
end
class SurveySectionsController < ActionController::Base
def current_user; User.first; end
def new
@survey = current_user.surveys.find params[:survey_id]
@survey_section = @survey.survey_sections.new
render text: "teh #{@survey_section.inspect}"
end
def create
@survey = current_user.surveys.find(params[:survey_id])
@survey_section = @survey.survey_sections.create(params[:survey_section])
redirect_to admin_surveys_survey_sections_path(@survey, @survey_section)
end
def show
@survey = current_user.surveys.find(params[:survey_id])
@survey_section = @survey.survey_sections.find(params[:id])
render text: "wat #{@survey_section.inspect}"
end
# index update destroy
end
end
SurveyApp::Application.initialize!
run SurveyApp::Application
source :rubygems
gem 'actionpack', '3.2.3'
gem 'activerecord', '3.2.3'
gem 'activesupport', '3.2.3'
gem 'railties', '3.2.3'
gem 'tzinfo'
gem 'sqlite3'
gem 'thin'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment