我正在構建一個使用RABL的rails應用程序,該應用程序在Active Admin控制的數據上生成一個API。這是一個非常簡單的API,我沒有太多數據,但是我對Angular.js和客戶端應用程序更新。使用rabl,我在2個對象上創建一個API。這是我的代碼到目前爲止;如何使用Angular.js來使用Rails RABL API?
project.rb
class Project < ActiveRecord::Base
has_attached_file :image, :styles => { :small => "100x100", :medium => "200x200", :large => "300x300" },
:url => "/system/members/:id/:style/:basename.:extension",
:path => ":rails_root/public/system/members/:id/:style/:basename.:extension"
validates_attachment :image, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png"] }
end
projects_controller.rb
class ProjectsController < ApplicationController
def index
@projects = Project.all
respond_to do |format|
format.json { render }
end
end
端
視圖/項目/ index.html.rabl
collection @projects
attributes :id, :title, :short_description, :long_description, :client, :image
member.rb
class Member < ActiveRecord::Base
has_one :image
has_attached_file :image, :styles => { :small => "100x100", :medium => "200x200", :large => "300x300" },
:url => "/system/members/:id/:style/:basename.:extension",
:path => ":rails_root/public/system/members/:id/:style/:basename.:extension"
validates_attachment :image, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png"] }
end
members_controller.rb
class MembersController < ApplicationController
def index
@members = Member.all
respond_to do |format|
format.json { render }
end
end
end
視圖/構件/ index.html.rabl
collection @members
attributes :id, :name, :title, :bio, :facebook, :twitter, :instagram, :linkedin, :dribbble, :image
角度;
應用程序/資產/ Java腳本/ app.js
var app = angular.module("onyx", ["ngResource"]);
app.factory("Member", [ //member for the member object
"$resource", function($resource) {
return $resource("/member/:name", { //trying to just start by getting the name
name: "@name"
}, {
update: {
method: "GET"
}
});
}
]);
更新: 由於this夢幻般的教程中,我得到了一切工作。
我更喜歡Restangular的樂趣:https://github.com/mgonto/restangular –