This guide is work in progress!
About Rails
From its site: "Ruby on Rails is an open-source web framework that's optimized for programmer happiness and sustainable productivity. It lets you write beautiful code by favoring convention over configuration."
Installation
Make sure you have installed Rails and the sqlite gem
$ sudo gem install sqlite3-ruby
$ sudo gem install rails
Create the project
A small rails application will serve as a backend for the Todos application.
Create a new Todos app by entering the following command into the console:
$ rails todos
$ cd todos
Make sure that you do issue this command outside your SproutCore application folder!
Setup the data model
We can use Rails generators to greate a basic RESTful interface for the Task model by typing
$ script/generate scaffold Task description:string isDone:boolean order:integer
Now you need to update your database to conform the model. For that, type
$ rake db:migrate
Customize the JSON output
Sproutcore uses the field guid for objects ids, but Rails calls this field id. To customize the JSON output of an object, we'll write a json_to_task protected method in TasksController (app/controllers/tasks_controller.rb):
protected
def json_for_task(task)
{ :guid => task_path(task),
:description => task.description,
:isDone => task.isDone
}
end
Writing the controller
Open the file app/controllers/tasks_controllers.rb, and modify the index method like this:
def index
@tasks = Task.all
respond_to do |format|
tasks = @tasks.map {|task| json_for_task(task) }
format.json { render :json => { :content => tasks } }
format.html
format.xml { render :xml => @task }
end
end
Start the server from the Rails app directory:
$ script/server
To fill your database, create some tasks by visiting http://localhost:3000/tasks and entering some test data.
To test your server now, open the following address in your web browser: http://localhost:3000/tasks?format=json. You should receive a plain-text-file, with the datafields of your test data.
Implementing CRUD operations
The following sections show how to implement the CRUD (create, read, update, delete) operations for our Task model.
We will start with read (or also GET), as it is the easiest. Extend the code like this:
Read or GET Task
def show
@task = Task.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @task }
format.json do
render :json => {
:content => json_for_task(@task),
:location => task_path(@task)
}
end
end
end
Try the outcome by calling http://localhost:3000/tasks/show/1?format=json.
Create Task
def create
@task = Task.new(params[:task])
respond_to do |format|
if @task.save
flash[:notice] = 'Task was successfully created.'
format.json do
render :json => { :content => json_for_task(@task) }, :status => :created,
:location => task_path(@task)
end
format.html { redirect_to(@task) }
format.xml { render :xml => @task, :status => :created, :location => @task }
else
format.html { render :action => "new" }
format.xml { render :xml => @task.errors, :status => :unprocessable_entity }
end
end
end
Update Task
def update
@task = Task.find(params[:id])
respond_to do |format|
params[:task].delete(:guid)
if @task.update_attributes(params[:task])
flash[:notice] = 'Task was successfully updated.'
format.json do
render :json => { :content => json_for_task(@task) }, :location => task_path(@task)
end
format.html { redirect_to(@task) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @task.errors, :status => :unprocessable_entity }
end
end
end
Delete Task
def destroy
@task = Task.find(params[:id])
@task.destroy
respond_to do |format|
format.json { head :ok }
format.html { redirect_to(tasks_url) }
format.xml { head :ok }
end
end
Setup your proxy
Add the following line to the Buildfile file in your SproutCore project:
proxy "/tasks", :to => "localhost:3000"
You’re Done!
Continue to next step: Step 7: Hooking Up to the Backend »
Comments (0)
You don't have permission to comment on this page.