我是新來的rails,所以對不起,如果有時我沒有太大的意義。這是我想要做的。我正在嘗試建立投票系統。因此,在博客文章旁邊有一個鏈接,表示「投票」(稍後可能會這樣說)。到目前爲止,我一直在努力:當點擊投票按鈕時,值爲'1'被傳遞到投票表格,然後通過AJAX(我複製了評論功能)投票表決下的投票記錄顯示。我不想渲染所有數字'1',而是想要渲染更新的計數。顯示記錄數 - Ruby on Rails - Ajax
我的投票表有成功輸入的列'投票'和'post_id'。我的想法是,我可以改變我的部分模板來做到這一點。下面是代碼:
votes_controller:
class VotesController < ApplicationController
def create
@post = Post.find(params[:post_id])
@vote = @post.votes.create!(params[:vote])
respond_to do |format|
format.html { redirect_to @post}
format.js
end
end
end
def count
@post = Post.find(params[:post_id])
@vote = calculate :count
respond_to do |format|
format.html { redirect_to @post}
format.js
end
end
end
這裏是頁其中正在顯示,/posts/show.html.erb:
<div id="backto"<%= link_to 'Back to all BattleCries', posts_path %></div>
<%= render :partial => @post %><br/>
<p5>Add a Comment</p5>
<div id="belt">
<div id="belttext">
<% remote_form_for [@post, Comment.new] do |f| %>
<p>
<%= f.text_area (:body, :class => "commentarea") %>
</p>
<%= f.submit "Add Comment"%>
<% end %>
</div>
<div id="beltbottom">
</div>
</div><br/>
<br/><p5>Comment Stream </p5>
<div id="comments">
<%= render :partial => @post.comments %>
</div>
<p>
<% remote_form_for [@post, Vote.new] do |f| %>
<p>
<%= f.hidden_field :vote, :value => '1' %>
</p>
<%= f.submit "Vote" %>
<% end %>
<div id="vote">
<div id="votes">
<%= render :partial => @post.votes %>
</div>
</div>
</p>
這裏是:局部,/ vote/_vote.html.erb :(這是我認爲我只需將其更改爲vote.count或post.count或其他東西但無法使其工作的地方)。
<% div_for vote do %>
<%= h(vote.vote) %>
<% end %>
這裏是/votes/create.js.rjs文件:
page.insert_html :bottom, :votes, :partial => @vote
page[@vote].visual_effect :highlight
我希望所有有意義。
它確實但重複它。 3 – bgadoci 2009-11-19 21:15:59