在javascript中调用模型方法

时间:2013-06-28 12:46:46

标签: javascript ruby-on-rails model token faye

这是我在Ruby on Rails 3项目中的模型:

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 个答案:

答案 0 :(得分:0)

你必须在某个地方渲染你的channel

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

而不仅仅是

$('#channel_id').val();