Ruby on rails Rails中的Coffeescript绑定
我目前正在Rails 4应用程序中使用以下示例应用程序实现webrtc+pubnub大厅: 除了html和coffeescript之间的绑定之外,我已经成功地实现了所有部分 在我的chatroom.html.erb中,我有以下内容:Ruby on rails Rails中的Coffeescript绑定,ruby-on-rails,coffeescript,erb,Ruby On Rails,Coffeescript,Erb,我目前正在Rails 4应用程序中使用以下示例应用程序实现webrtc+pubnub大厅: 除了html和coffeescript之间的绑定之外,我已经成功地实现了所有部分 在我的chatroom.html.erb中,我有以下内容: <div class="box"> <h3>Try Now</h3> <input type="text" id="userid" placeholder=
<div class="box">
<h3>Try Now</h3>
<input type="text" id="userid" placeholder="Username" />
<br />
<a href="#" class="btn btn-large btn-inverse" id="login">Log In</a>
<br />
<p style="padding-top: 12px;">- or -</p>
<span id="signinButton"></span>
</div>
<script type="text/html" id="user-item-template">
<li class="user" data-user="<%= id %>">
<span class="name"><%= name %></span>
<a href="#" class="btn btn-success" data-user="<%= id %>">Call</a>
</li>
</script>
document.querySelector('#login').addEventListener 'click', (event) ->
uuid = document.querySelector('#userid').value
login "guest-#{uuid}"
login = (name) ->
uuid = name
userTemplate = _.template $("#user-item-template").text()
userList = $("#user-list")
$(document).on 'pubnub:ready', (event) ->
pubnub.subscribe
channel: 'phonebook'
callback: (message) ->
# Do nothing
presence: (data) ->
# {
# action: "join"/"leave"
# timestamp: 12345
# uuid: "Dan"
# occupancy: 2
# }
if data.action is "join" and data.uuid isnt uuid
newItem = userTemplate
name: data.uuid.split('-')[1]
id: data.uuid
userList.append newItem
else if data.action is "leave" and data.uuid isnt uuid
item = userList.find "li[data-user=\"#{data.uuid}\"]"
item.remove()
我得到以下错误:
内部服务器错误
未定义的局部变量或方法“id”
我假设我因erb标签而出现并发症?它们已经在js应用程序示例中定义。rails erb标记和javascript应用程序中的标记之间有区别吗?我注意到该示例仅使用普通html。也许像Angular.js这样的选项会有所帮助,但我不想深入研究
编辑:添加的输入表单接受用户id/name您的coffeescript文件的名称是否与_script.js.coffee.erb类似?是的@koyz coffeescript可以在rails中提供带有erb标记的绑定吗?id从何而来?@RubyRacer我已经更新并添加了分配给id的coffeescript我相信OK,这些是javascript变量,也不是ruby变量。。。您必须删除。你在使用客户端技术吗?仅仅通过提供价值,您将如何实现绑定?无论如何,您可能应该提供一个表达式,具体取决于客户端框架。例如,在Angular.js中,它是{{id}