如何在rails中调用javasscript_tag?
How to call javasscript_tag in rails?
我遵循了上述问题的每一步,这里是我的更改:
In show.html.erb (http://localhost:3000/posts/24)
<% javascript_tag do %>
$(function() {
setInterval(function(){ $.get("/posts/<%= @post.id %>"); }, 5000);
});
<% end %>
<span id="changed_data">
It should be change after every 5 seconds
</span>
控制器class
def show
@post = Post.find(params[:id])
respond_to do |format|
format.js
format.html
end
结束
添加了一个新文件
app/views/posts/show.js.erb
$("#changed_data").html("<h2> Changed </h2>");
但听起来 javascript_tag
没有调用 jquery
方法。我在这里做错了什么?
试试这个:
<% javascript_tag do %>
$(function() {
setInterval(function(){ $.get("/posts/<%= @post.id %>"); }, 5000);
});
<% end %>
改为
<script>
$(function() {
setInterval(function(){ $.get("/posts/<%= @post.id %>"); }, 5000);
});
</script>
应该可以。
我遵循了上述问题的每一步,这里是我的更改:
In show.html.erb (http://localhost:3000/posts/24)
<% javascript_tag do %>
$(function() {
setInterval(function(){ $.get("/posts/<%= @post.id %>"); }, 5000);
});
<% end %>
<span id="changed_data">
It should be change after every 5 seconds
</span>
控制器class
def show
@post = Post.find(params[:id])
respond_to do |format|
format.js
format.html
end
结束
添加了一个新文件
app/views/posts/show.js.erb
$("#changed_data").html("<h2> Changed </h2>");
但听起来 javascript_tag
没有调用 jquery
方法。我在这里做错了什么?
试试这个:
<% javascript_tag do %>
$(function() {
setInterval(function(){ $.get("/posts/<%= @post.id %>"); }, 5000);
});
<% end %>
改为
<script>
$(function() {
setInterval(function(){ $.get("/posts/<%= @post.id %>"); }, 5000);
});
</script>
应该可以。