📜 ⬆️ ⬇️

Rails 3 and SproutCore

Hello! There is such a wonderful javascript framework called SproutCore . With it, you can quite easily write web applications with a desktop-like interface. The thing is quite popular, for example, Apple is used in iWork.com . Under the cut, we screw SproutCore to the last rails.

Of course, first put the gem with SproutCore:
sudo gem install sproutcore

By the way, the version of RubyGems should not be below 1.2, otherwise it will not work. But upgrading is easy enough:
sudo gem update --system

Ok, we will assume that heme is worth it. In principle, you can try SproutCore right now:
sc-init hello_sp
cd hello_sp
sc-server

Then go to localhost: 4020 and see the result. But we are going to screw all this joy to the rails.
The official site provides an example with a todo RESTful application, so let's not invent anything and repeat:

Create a project:
$ rails new todos
$ cd todos

Next, we need to create and fill the database with something, plus editing this data via the web interface. Scaffolding is not the best solution, but it is suitable for quick testing:
rails g scaffold Task description:string isDone:boolean order:integer

Apply migrations:
rake db:migrate

Next, we need to work a little on the implementation of standard CRUD operations, bring app / controllers / tasks_controllers.rb to the following form:
 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 fill the database with something. Many options, one of the most convenient - fill 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 some values), you can also drive directly into the console: rails c and forward. Next, fill out our database:
rake db:seed

Everything is almost ready for the application rails test, it remains to remove protect_from_forgery from ApplicationController, otherwise a terrible error “InvalidAuthenticityToken error” may occur.

Start:
rails s

Under 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 is invited to write a bulky class, but in the comments suggested a more correct solution.
So, let's change our ActiveRecord :: Base 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 for example:
 store.loadRecords(Todos.Task, response.get('body')); 


It remains to add a line to the buildfile file of our project:
 proxy "/tasks", :to => "localhost:3000" 


That's all for now. Documentation and articles on SproutCore unfortunately not so much, but the framework clearly deserves attention.

Project site
Demo interface and more
Project Wiki
Album video on sabzh on Vimeo

Source: https://habr.com/ru/post/116288/


All Articles