ruby on rails - Socialization likes -
i start use socialization gem. so, created user model devise:
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
then created post scaffold:
class post < activerecord::base belongs_to :user acts_as_likeable end
and want allow user posts. don't know how create view button, dont know how write methods likes. please give me little example. i'm new in rails
i create link in veiw/posts/show.html.erb
.
<%= link_to "like", like_post_path(@post), :method => :post, :class => 'btn btn-primary btn-xs' %>
and method in app_contoller:
def @post = post.find(params[:id]) current_user.like!(@post) end
how write route this?
you can test in console see how works first: rails c
user = user.first post = post.first user.like!(post) user.likes?(post)
so can create action: likes
in posts controller.
def likes @user = current_user # before_action :authenticate_user, only: [:likes] @post = post.find(params[:id]) @user.like!(@post) redirect_to :back, notice: "liked post successfully!" end
and create route action:
get 'post/:id/likes', to: 'posts#likes', as: :likes
and in views:
<%= link_to 'like', likes_path(@post) %>
Comments
Post a Comment