我正在尝试在我的rails应用中使用Firebase。我希望我的应用中的所有CRUD操作都映射到Firebase中的关联记录。也就是说,如果我添加,编辑或删除“共享”,我希望它在Firebase和通过javascript呈现的HTML中进行更新。我在方法update_firebase和after_destroy中添加了什么?如何告诉firebase从数据库中选择哪条记录?
我正在使用firebase-ruby gem。
以下是我的文件:
#config/initializers/firebase.rb
require 'firebase'
require 'rubygems'
require "firebase_token_generator"
base_uri = 'https://innohack-7bb17.firebaseio.com'
FIREBASE = Firebase::Client.new(base_uri)
payload = {:uid => "1", :auth_data => "foo", :other_auth_data => "bar"}
options = {:admin => true}
generator = Firebase::FirebaseTokenGenerator.new("YOUR_FIREBASE_SECRET")
token = generator.create_token(payload, options)
#app/models/share.rb
class Share < ActiveRecord::Base
after_create :push_to_firebase
after_save :update_firebase
after_destroy :destroy_from_firebase
include SimpleHashtag::Hashtaggable
validates :body, presence: true
belongs_to :user, foreign_key: 'user_id', class_name: "User"
scope :newest, -> {order("updated_at DESC")}
has_many :votes, dependent: :destroy
has_many :comments, dependent: :destroy
private
def push_to_firebase
response = FIREBASE.push("shares", {
:title => self.title,
:body => self.body,
:lat => self.lat,
:lon => self.lon
})
end
def update_firebase
end
def after_destroy
end
end
#app/views/shares/index.html.erb
<script src="https://cdn.firebase.com/js/client/1.0.18/firebase.js"></script>
<script src="http://www.webglearth.com/v2/api.js"></script>
<script>
$(function() {
if (gon.shares.length != 0) {
var shares = gon.shares;
var options = {
center: [shares[shares.length-1].lat, shares[shares.length-1].lon],
sky: false,
atmosphere: false,
zoom: 3,
tilting: true,
dragging: true,
};
} else {
var options = {
sky: false,
atmosphere: false,
zoom: 3,
tilting: true,
dragging: true,
};
};
earth = new WE.map('earth_div', options);
WE.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(earth);
var myFirebaseRef = new Firebase("https://innohack-7bb17.firebaseio.com");
myFirebaseRef.child("shares").on("child_added", function(snapshot) {
var lat = snapshot.val().lat;
var lon = snapshot.val().lon;
var latLon = [lat, lon];
var title = snapshot.val().title;
var body = snapshot.val().body;
var trimmedBody = body.substring(0, 25);
var trimmedBody2 = body.substring(25, 100);
var marker = WE.marker(latLon).addTo(earth);
marker.bindPopup("<b>" + title + "</b><br>" + trimmedBody + "<br /><span style='font-size:10px;color:#999'>" + trimmedBody2 + "...</span>");
});
});
</script>
<div class="row">
<div class="col-xs-12">
<h1>Shares</h1>
<br />
</div>
</div>
<div class="row">
<div id="earth_div"></div>
</div>
<div class="row">
<div class="col-xs-10 col-xs-offset-1 col-sm-8 col-sm-offset-2 col-md-6 col-md-offset-3">
<% @shares.each do |share| %>
<div class="booyah-box post">
<div class="handle">
<h4><%= link_to share.title, share_path(share) %></h4>
<p>
<%= linkify_hashtags(truncate_html(markdown(share.body), length: 100)) %>
</p>
<small><%= share.user.name %></small>
<p><%= pluralize(share.votes.count, "votes") %></p>
</div>
<br />
<br />
<% share.comments.first(3).each do |comment| %>
<blockquote>
<%= comment.body %><br />
<small><%= comment.user.email %></small>
</blockquote>
<% end %>
<br />
<% if share_owner?(share) %>
<div>
<div class="btn btn-danger">
<%= link_to 'Delete', share_path(share), method: :delete, data: {confirm: 'Are you sure?'} %>
</div>
<div class="btn btn-warning">
<%= link_to 'Edit', edit_share_path(share) %>
</div>
</div>
<% end %>
</div>
<% end %>
</div>