Easily setup and use Spine with Rails 3.1.
This gem requires the use of Rails 3.1, CoffeeScript and the new Rails asset pipeline provided by Sprockets.
This gem does two things:
-
Adds Spine to the asset pipeline, so you can easily require it in your applications:
//= require spine
-
Adds some Spine generators, so you can easily create Spine Models, Views and Controllers.
In your Gemfile, add the following lines:
gem "json2-rails"
gem "spine-rails"
Make sure that your Gemfile also includes
gem "jquery-rails"
Or if your using Zepto
gem "zepto-rails"
Then run the following commands:
bundle install
rails generate spine:new
Running rails g spine:new
will create the following directory structure:
app/assets/javascripts/app/models/
app/assets/javascripts/app/views/
app/assets/javascripts/app/controllers/
app/assets/javascripts/app/index.js.coffee
By default your application will be namespaced by the app
directory. You can specify a different namespace with the --app
option:
rails g spine:new --app foo_bar
NOTE: If you use the --app
option here, then you will also have to specify it with other generators.
Use the top-level level index.js.coffee
file to setup namespacing and initial controller instantiation.
spine-rails provides three simple generators to help you get started:
rails g spine:model User email username full_name
This generator creates a very minimal model inside app/assets/javascript/app/models
. You have to provide a list of attributes for the model.
rails g spine:controller Users
This generator creates a minimal Users
controller in app/assets/javascripts/app/controllers
to get you started.
rails g spine:view users/index
This generator creates a blank Spine view app/assets/javascripts/app/views/users/index.jst.ejs
.
The generator will create views in hamljs
, eco
or ejs
format, depending on the gems availale:
- eco - will use ECO templates
- ruby-haml-js - will use HAMLJS templates
- otherwise, EJS templates will be used
Created a new Rails application called blog
.
rails new blog
Edit your Gemfile and add
gem "spine-rails"
Install the gem and generate resource.
bundle install
rails g scaffold Post title:string content:string
rake db:migrate
rails g spine:new
rails g spine:model Post title content
rails g spine:controller Posts
You now have the default Spine data structures available to work with.
Next navigate to http://localhost:3000/posts, and open up the JavaScript console in the browser.
Now you can use Spine:
// Sends an AJAX POST to the server
var post = App.Post.create({
title: 'Hello World!',
content: 'Spine & Rails, sitting in a tree!'
});
// => ID returned from Rails
post.id;
// Sends AJAX PUT to the server
post.updateAttributes({title: 'Goodbye'});
Reload the page, then:
App.Post.fetch(); // Fetch all posts
App.Post.first().content;
For more information on how to integrate Spine with Rails, please see the Rails guide.
Also if you want to have some useful helpers to bridge the gap between Spine and Rails, then spine-extensions is for you.
This plugin was made by Alex MacCaw with major contributions from Dmytrii Nagirniak [Zohar Arad] (https:://github.com/zohararad) and [Ben Haines] (http://github.com/benhainez). It's under the same license as Spine (MIT).