ruby-on-rails-3 – 如何使用Ajax / JavaScript(Rails)动态更新
发布时间:2020-12-17 02:35:17 所属栏目:百科 来源:网络整理
导读:我建立了一个简单的投票系统: votes_controller.rb: class VotesController ApplicationController def vote_up @post = Post.find(params[:id]) @vote = @post.votes.create(:user_id = current_user.id,:polarity = 1) endend (如果这里有任何不良做法,
我建立了一个简单的投票系统:
votes_controller.rb: class VotesController < ApplicationController def vote_up @post = Post.find(params[:id]) @vote = @post.votes.create(:user_id => current_user.id,:polarity => 1) end end (如果这里有任何不良做法,请告诉我) 意见/ show.html.erb: <h3><%= @post.votes.count %> votes</h3><br /> <%= link_to "Vote Up",vote_up_path(@post),:remote => true %> (我当然会把这部分放在一边) routes.rb中: get 'votes/:id/vote_up' => 'votes#vote_up',as: 'vote_up' 当我点击“投票”链接时,投票会添加到帖子中,但我必须刷新页面才能看到更改.如何使用Ajax / JavaScript动态刷新@ post.votes.count和vote.user.username? 解决方法
首先为show.html.erb制作一个包装div
<div class='post-<%=@post.id%>' > <h3><%= @post.votes.count %> votes</h3><br /> <%= link_to "Vote Up",:remote => true %> </div> 并在vote_up.js.erb中 $("post-<%=@post.id%>").html('<%=escape_javascript @post.votes.count %>'); 或类似的东西 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读