按投票排序微博
我不知道如何格式化这里的代码。我在这里重新列出了这个问题(以更好的方式): http://railsforum.com/viewtopic.php? id=43839
拥有一个具有投票功能的简单微帖子网站。我希望能够按 votes.size 显示发布的微帖子。我似乎无法让它发挥作用,希望得到一些指导。
模型:
class Micropost
attr_accessible :content
has_many :votes
belongs_to :user
default_scope :order => 'microposts.created_at DESC'
class Vote
belongs_to :micropost
视图:
# microposts page
<% unless @picture.microposts.empty? %>
<table class="microposts" summary="User microposts">
<%= render @microposts %>
</table>
<% end %>
# microposts partial
<tr>
<td class="micropost">
<span class="gravatar">
<%= link_to gravatar_for(micropost.user, :size => '60'), micropost.user %>
</span>
<span class="content" >
<%= micropost.content %>
</span>
<span id="vote_total_<%= micropost.id %>" class="micropost_votes">
votes:<%= micropost.votes.size %>
</span>
<% if current_page?(root_path) && signed_in? %>
<span id="vote_button">
<%= button_to 'vote', micropost_votes_path(:micropost_id => micropost),
:remote => true
%>
</span>
<% end %>
</td>
</tr>
控制器:
# VotesController
def create
@micropost = Micropost.find(params[:micropost_id])
@vote = @micropost.votes.build params[:micropost_id]
respond_to do |format|
if @vote.save
format.html { redirect_to @micropost }
format.js
else
format.html { redirect_to root }
end
end
end
我已经能够按created_at日期对微帖子进行排序,但我似乎无法按micropost.votes.size对微帖子进行排序。这似乎与根据不同模型的状态订购一个模型有关。
I can't figure out how to format the code here. I relisted the question here (in nicer fashion): http://railsforum.com/viewtopic.php?id=43839
Have a simple microposts site with voting functionality. I want to be able to display the posted microposts by votes.size. I can not seem to get it to work and would appreciate some guidance.
MODELS:
class Micropost
attr_accessible :content
has_many :votes
belongs_to :user
default_scope :order => 'microposts.created_at DESC'
class Vote
belongs_to :micropost
VIEWS:
# microposts page
<% unless @picture.microposts.empty? %>
<table class="microposts" summary="User microposts">
<%= render @microposts %>
</table>
<% end %>
# microposts partial
<tr>
<td class="micropost">
<span class="gravatar">
<%= link_to gravatar_for(micropost.user, :size => '60'), micropost.user %>
</span>
<span class="content" >
<%= micropost.content %>
</span>
<span id="vote_total_<%= micropost.id %>" class="micropost_votes">
votes:<%= micropost.votes.size %>
</span>
<% if current_page?(root_path) && signed_in? %>
<span id="vote_button">
<%= button_to 'vote', micropost_votes_path(:micropost_id => micropost),
:remote => true
%>
</span>
<% end %>
</td>
</tr>
CONTROLLERS:
# VotesController
def create
@micropost = Micropost.find(params[:micropost_id])
@vote = @micropost.votes.build params[:micropost_id]
respond_to do |format|
if @vote.save
format.html { redirect_to @micropost }
format.js
else
format.html { redirect_to root }
end
end
end
I've been able to sort the microposts by the created_at date, but I can not seem to sort the microposts by the micropost.votes.size. It seems to have something to do with ordering one model based on the state of a different model.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,这是我找到的最好的解决方案。在渲染微帖子的视图上,我添加了:
所以实际的渲染代码如下所示:
我实际上颠倒了我的帖子,以便投票总数最高的帖子位于顶部。这很简单:
我不知道这是否会造成数据库性能问题,但它似乎运行良好。
Well, this was the best solution that I found. On the view where the microposts are rendered, I added:
So the actual render code looks like this:
I actually reversed my posts so that the posts with the highest vote totals were on top. This is easy:
I have NO idea if this will create problems with database performance, but it seems to be working well.