2013-06-28 63 views
0

这是我的Ruby模型on Rails的3项目:呼叫模型方法在JavaScript

class Faye < ActiveRecord::Base 
    def faye_chatroom_token_id 
    a = Faye.find_by_sql("SELECT userid FROM chat WHERE id = #{current_profile.id}") 
    Digest::MD5.hexdigest(a.first) 
    end 
end 

我需要在JavaScript中使用它:

function broadcast(){ 
    var channel = **Hier I will call the model method**; 
    $.ajax({ 
    type: 'POST', 
    url: "http://localhost:9292/faye", 
    data: 'message={"channel":"/'+channel+'/chatroom", "data":"1"}', 
    dataType: "json", 
    xhrFields: {withCredentials: true}, 
    crossDomain: true, 
    }); 
} 

请帮帮我!

+1

(在哪里)JS如何呈现? – okliv

回答

0

你必须从某个地方呈现到HTML您channel

<%= hidden_field_tag :channel_id, @faye.faye_chatroom_token_id %> 

,并不仅仅是把它与

​​