ruby on rails - acts_as_votable using ip address -
i want allow users vote on posts without signing up, , have vote instead tied ip address. tried following this post need more clarification. error im getting posts_controller
undefined method `find_or_create_by_ip' #
this code far:
posts_controller:
def upvote @post = post.find (params[:id]) session[:voting_id] = request.remote_ip upvote = session.find_or_create_by_ip(session[:voting_id]) @post.upvote redirect_to :back end def downvote @post = post.find (params[:id]) session[:voting_id] = request.remote_ip downvote = session.find_or_create_by_ip(session[:voting_id]) @post.downvote redirect_to :back end
session model:
class session < activerecord::base acts_as_voter request.remote_ip end
routes.rb:
rails.application.routes.draw 'static_pages/home' 'static_pages/about' resources :posts member put "like" , to: "posts#upvote" put "dislike" , to: "posts#downvote" end end
in other post said add ip column sessions table. mean? saw somewhere need create db table well. i? , how that? sorry having post this, put im pretty new this, great if spelled out better me. thanks!
if using rails 4+ dynamic finders have been deprecated , extracted gem. standard find_or_create_by or find_or_initialize_by.
def upvote @post = post.find (params[:id]) session[:voting_id] = request.remote_ip upvote = session.find_or_create_by(ip: session[:voting_id]) @post.upvote redirect_to :back end def downvote @post = post.find (params[:id]) session[:voting_id] = request.remote_ip downvote = session.find_or_create_by(ip: session[:voting_id]) @post.downvote redirect_to :back end class session < activerecord::base acts_as_voter request.remote_ip end
Comments
Post a Comment