我有用户,帖子,投票模型。这是我的投票模型:
class Vote < ActiveRecord::Base
SCORE_REGEX = /-1|1/
# Relations
belongs_to :user
belongs_to :post
# Validations
validates :score,
allow_nil: true,
format: {with: SCORE_REGEX }
end
我的帖子显示在主页和用户个人资料上。我的控制器是:static_pages_controller(这是控制器保存的主页),users_controller,posts_controller,votes_controller。我在views / posts / _post.html.erb中有张贴模板,看起来像这样:
<%= link_to post.id, post %> <br>
<%= link_to post.user.name, post.user %>
<%= post.content %>
posted <%= time_ago_in_words(post.created_at) %> ago. <br>
<% if post.edited %>
post has been edited <%= time_ago_in_words(post.updated_at) %> ago.
<% end %>
<% if current_user?(post.user) %>
<%= link_to "delete", post, method: :delete %>,
<%= link_to "edit", edit_post_path(post)%>
<% end %>
<%= render 'shared/vote_form' %>
如何为这些帖子设置一个有效的投票按钮(例如,不喜欢,不喜欢,不喜欢)?
是什么让你使用像宝石acts_as_votable?
关于按钮,可以简单地link_to
使用method: 'post'
和remote: true
。链接的目标,在不知道您的路线的情况下不能说。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句