
Rails 3 and SproutCore
Hello! There is such a wonderful JavaScript framework called SproutCore . With it, you can quite easily write web applications with an approximation to the desktop interface. The thing is quite popular, used for example by Apple at iWork.com . Under the cut, we fasten SproutCore to the last rails.
Of course, first we’ll install a gem with SproutCore:
By the way, the RubyGems version must be at least 1.2, otherwise nothing will work. But updating is easy enough:
Ok, let's assume that we have a gem. In principle, you can try SproutCore now: After that, go to localhost: 4020 and see the result. But we will be screwing all this joy to the rails.
An example with a todo RESTful application is provided on the official website, so we won’t invent anything and repeat:
Create a project: Next, we need to create and populate the database with something, plus editing this data via the web interface. Scaffolding is not the best solution, but it’s suitable for quick testing: Apply migrations: Next, we need to work a bit on the implementation of standard CRUD operations, bring app / controllers / tasks_controllers.rb to the following form:
Great, the next step is to populate the database with something. There are many options, one of the most convenient is to fill out db / seeds.rb:
Although (if there are few values), you can also drive directly into the console:
Everything is almost ready for the application rails test, it remains to be removed
Start:
At this link you should observe the text with the contents of your database.
It remains to add a little SproutCore to the project, it is not so difficult - you need to provide the correct data in JSON format. The manual suggests writing a bulky class, but the comments suggested a more correct solution.
So, let's change our ActiveRecord :: Base in the project, it should look something like this:
Accordingly, you can use it for example like this:
It remains to add a line to the buildfile of our project:
That's all for now. Unfortunately, there are not so many documentation and articles on SproutCore, but the framework clearly deserves attention.
Project site
Demo interface and another
Wiki project
Album video on a subject on Vimeo
Of course, first we’ll install a gem with SproutCore:
sudo gem install sproutcore
By the way, the RubyGems version must be at least 1.2, otherwise nothing will work. But updating is easy enough:
sudo gem update --system
Ok, let's assume that we have a gem. In principle, you can try SproutCore now: After that, go to localhost: 4020 and see the result. But we will be screwing all this joy to the rails.
sc-init hello_sp
cd hello_sp
sc-server
An example with a todo RESTful application is provided on the official website, so we won’t invent anything and repeat:
Create a project: Next, we need to create and populate the database with something, plus editing this data via the web interface. Scaffolding is not the best solution, but it’s suitable for quick testing: Apply migrations: Next, we need to work a bit on the implementation of standard CRUD operations, bring app / controllers / tasks_controllers.rb to the following form:
$ rails new todos
$ cd todos
rails g scaffold Task description:string isDone:boolean order:integer
rake db:migrate
class TasksController < ApplicationController
respond_to :json
def index
respond_with(@tasks = Task.all)
end
def show
respond_with(@task = Task.find(params[:id]))
end
def create
respond_with(@task = Task.create(:description => params[:description],
:isDone => params[:isDone],
:order => params[:order]))
end
def update
@task = Task.find(params[:id])
@task.description = params[:description]
@task.isDone = params[:isDone]
@task.order = params[:order]
@task.save
respond_with(@task)
end
def destroy
@task = Task.find(params[:id])
@task.destroy
render(:nothing => true, :status => :ok)
end
end
Great, the next step is to populate the database with something. There are many options, one of the most convenient is to fill out db / seeds.rb:
Task.create(:description => 'This is the first task', :isDone => true, :order => 1)
Task.create(:description => 'This is the second task', :isDone => false, :order => 2)
Task.create(:description => 'This is the third task', :isDone => true, :order => 3)
Although (if there are few values), you can also drive directly into the console:
rails c
forward. Next, fill out our database: rake db:seed
Everything is almost ready for the application rails test, it remains to be removed
protect_from_forgery
from the ApplicationController, otherwise a terrible “InvalidAuthenticityToken error” error may occur. Start:
rails s
At this link you should observe the text with the contents of your database.
It remains to add a little SproutCore to the project, it is not so difficult - you need to provide the correct data in JSON format. The manual suggests writing a bulky class, but the comments suggested a more correct solution.
So, let's change our ActiveRecord :: Base in the project, it should look something like this:
class Task < ActiveRecord::Base
def as_json(options = {})
ret = {
:guid => "/tasks/#{self.id}",
:id => self.id,
:description => self.description,
:isDone => self.isDone
}
end
end
Accordingly, you can use it for example like this:
store.loadRecords(Todos.Task, response.get('body'));
It remains to add a line to the buildfile of our project:
proxy "/tasks", :to => "localhost:3000"
That's all for now. Unfortunately, there are not so many documentation and articles on SproutCore, but the framework clearly deserves attention.
Project site
Demo interface and another
Wiki project
Album video on a subject on Vimeo