我有一个投票系统,我希望投票响应是异步的。
在我的“展示”视图中,我有......
<%= render 'score' %>
呈现
<% if @user.voted?(@lesson) %>
<%= render 'voted' %>
<% else %>
<%= render 'unvoted' %>
<% end %>
进而呈现我的 _voted.html.erb
#will display different images, colored or not based on selection
<% if @user.up_voted?(@lesson) %>
<div class = "up_arrow">
<%= link_to image_tag("up_color.png", alt: "Uncheck vote"),
clear_vote_lesson_url(@lesson), :method => :post, remote: true %>
</div>
<div class="average_score"><%= @lesson.votes %></div>
<div class= "down_arrow">
<%= link_to image_tag("down_uncolor.png", alt: "Vote down"),
down_vote_lesson_url(@lesson), :method => :post, remote: true %>
</div>
<!-- user down voted then -->
<% else %>
<div class = "up_arrow">
<%= link_to image_tag("up_uncolor.png", alt: "Vote up"),
up_vote_lesson_url(@lesson), :method => :post, remote: true %>
</div>
<div class="average_score"><%= @lesson.votes %></div>
<div class= "down_arrow">
<%= link_to image_tag("down_color.png", alt: "Uncheck vote"),
clear_vote_lesson_url(@lesson), :method => :post, remote: true %>
</div>
<% end %>
我的 unvoted.html.erb 是
<div class = "up_arrow">
<%= link_to image_tag("up_uncolor.png", alt: "Vote up"),
up_vote_lesson_url(@lesson), :method => :post, remote: true %>
</div>
<div class="average_score"><%= @lesson.votes %></div>
<div class= "down_arrow">
<%= link_to image_tag("down_uncolor.png", alt: "Vote down"),
down_vote_lesson_url(@lesson), :method => :post, remote: true %>
</div>
然后最后在我的控制器中我有..
def up_vote
lesson = Lesson.find(params[:id])
current_user.up_vote!(lesson)
respond_to do |format|
format.html { render :action => 'show'}
format.js { ???????? }
end
end
我还有一些动作,例如 down_vote,但它非常相似。
我想我不太确定在我的 format.js 块中放什么。我尝试将诸如 redirect_to course_path(lesson) 之类的东西放入并渲染“分数”,但投票图像没有得到更新。
有人可以帮忙吗?谢谢!
更新@dhoelzgen
我把它改成...
def up_vote
lesson = Lesson.find(params[:id])
current_user.up_vote!(lesson)
render :score, :layout => false
#redirect_to lesson_path(lesson)
end
我也将渲染 :score, :layout => false 放在我的其他操作中。
在我看来我有
<div id="surrounding_container">
<%= render 'score' %>
</div>
随着
$('.up_arrow')
.bind 'ajax:success', (event, data) ->
$('#surrounding_container').html($(data))
$('.down_arrow')
.bind 'ajax:success', (event, data) ->
$('#surrounding_container').html($(data))
在我的课程.js.coffee 中。
我错过了什么吗?图像仍然不会异步更改