0

I'm trying to be able to upvote using acts_as_votable without the page reloading but I'm not sure how to change the routes to make it work

Controller

def upvote
    @video = Video.find(params[:id])
    @ip = request.remote_ip
    # Ipaddresstracker.delete_all
    if Ipaddresstracker.find_by(ipaddress: @ip)

    else
       Ipaddresstracker.create(:ipaddress => @ip, :upvoted => true, :upvotedcount => 1)
       @video.vote_by voter: User.first, :duplicate => true
    end
  # redirect_to :back
    respond_to do |format|
    format.html { redirect_to :back }
    format.js { render layout: false }
  end
end

View

<%= link_to like_video_path(x), method: :get, remote: true, class: "btn btn-default btn-sm" do %>

Route

resources :videos do
  member do
    put "like", to: "videos#upvote"
    put "dislike", to: "videos#downvote"
  end
end

Error

ActionController::RoutingError (No route matches [GET] "/videos/53/like"):

ChrisWilson
  • 459
  • 1
  • 6
  • 19

2 Answers2

1

The route expects a put request. Change the :method option to :put

<%= link_to like_video_path(x), method: :put, remote: true, class: "btn btn-default btn-sm" do %>
arieljuod
  • 15,460
  • 2
  • 25
  • 36
  • That gives the error ActionView::MissingTemplate (Missing template videos/upvote, application/upvote with {:locale=>[:en], :formats=>[:js, :html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]} I'm following this https://stackoverflow.com/questions/31251246/rails-acts-as-votable-gem-like-unlike-buttons-with-ajax but it doesn't specify what to do for the routes – ChrisWilson Mar 22 '19 at 19:59
  • You need to add a view for that action, like `views/views/upvote.js` (probably `.js.erb` so you can use your rails variables) with some javascript code to update the page. – arieljuod Mar 22 '19 at 20:16
  • There's some examples of the js view on the link you provided, play with that code, I don't know what's your actual view. – arieljuod Mar 22 '19 at 20:17
1

Actually, if you had Jquery you can solve this problem by Jquery. If not, try to change this format.js { render layout: false }. You need to render nothing so you can set render :nothing => true in your format.js block

IlyaOsotov
  • 126
  • 6