Skip to content

Instantly share code, notes, and snippets.

@aarongough
Created January 30, 2011 16:47
Show Gist options
  • Save aarongough/802997 to your computer and use it in GitHub Desktop.
Save aarongough/802997 to your computer and use it in GitHub Desktop.
content_for :js do
<!-- Output the base view-model code for our User resource. The view-model
will by default have client-side validations that mimic the server-side
model validations. It will also only expose interface methods for
attributes that are marked as accessible on the server-model
-->
<%= User.generate_view_model :update => :onchange %>
end
<!-- Now create the HTML elements that are bound to our view-model -->
<h1>Edit User:</h1>
<input data-binding="User.name" type="text" />
<input data-binding="User.email" type="text" />
<input data-binding="User.phone_number" type="text" />
content_for :js do
<!-- Output the collection view-model for the User resource. This model
manages the state of a collection of objects and allows the creation
of new items as well as the editing and deletion of existing ones.
-->
<%= User.generate_collection_view_model %>
end
<h1>Users:</h1>
<div id="user_container">
<!-- Now create the HTML template for our User items -->
<div data-template="User">
<span data-attribute="name"></span>
<span data-attribute="email"></span>
<span data-attribute="phone_number"></span>
<span data-action="destroy">Delete user</span>
</div>
<!-- Even though we've only created one item, because we've defined it as a
template Rails In Chains will take it and replicate it as many times as
necessary to represent all the items in the @users collection.
-->
</div>
<%= link_to "Create a new user", new_user_path %>
class User < ActiveRecord::Base
# Attributes:
# name
# email
# phone_number
validates_length_of :name, :within => 2..40
validates_length_of :email, :within => 6..255
validates_length_of :phone_number, :within => 6..40
validates_format_of :email, :with => EMAIL_REGEX
# We will be using this model with Rails In Chains
include RailsInChains::ServerModel
end
class UsersController < ApplicationController
# Rails In Chains provides a mixin that defines the standard
# CRUD controller actions for you (:index, :new, :create, :show, :update, :destroy)
include RailsInChains::ChainControllerMethods
# Tell Rails In Chains what the default resource for this
# controller is:
self.default_resource = User
# Tell the default CRUD actions which instance variable they
# should pass the resource information to the views through:
self.instance_variable = :user
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment