【发布时间】:2015-08-29 16:36:21
【问题描述】:
我开始使用Socialization gem。 因此,使用设计创建了用户模型:
class User < ActiveRecord::Base
has_many :posts
devise :database_authenticatable,
:registerable,
:recoverable,
:rememberable,
:trackable,
:validatable
acts_as_follower
acts_as_followable
acts_as_liker
end
然后我用脚手架创建了 Post:
class Post < ActiveRecord::Base
belongs_to :user
acts_as_likeable
end
我想允许用户喜欢帖子。但是我不知道如何用like按钮创建视图,也不知道如何编写like方法。请给我一个小例子。我是 Rails 新手
我在veiw/posts/show.html.erb 中创建链接。
<%= link_to "Like", like_post_path(@post),
:method => :post, :class => 'btn btn-primary btn-xs' %>
以及app_contoller中的方法:
def like
@post = Post.find(params[:id])
current_user.like!(@post)
end
如何为此编写路线?
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 social-media-like