ActionController::UrlGenerationError in Products#index error inacts_as_votable

问题描述

我正在尝试构建一个类似产品搜索的投票系统。我希望我的产品只获得点赞,每个产品都有一个与(routes.rb)相关的评论。我正在使用 act_as_votable gem 来做到这一点。但我收到此错误

 No route matches {:action=>"Vote",:controller=>"products",:id=>nil},missing required keys: [:id]

我的视图文件

</div>
  <%= link_to like_product_path(@product),class: "like-btn",method: :put,remote: :true do %>
    <div>
      <span>^ </span>
    </div>
    <% end %>
    <span class= "likes-count"><%= @product.get_upVotes.size %></span>
</div>

我的 Vote.js.erb 文件

<% if current_user.liked? @product %>
    $('.like-btn').addClass('liked');
<% else %>
    $('.like-btn').addClass('like-btn');
<% end %>
$('.likes-count').html("<%= @product.get_upVotes.size %>")

控制器文件

  before_action :set_product,only: [:Vote]
  before_action :authenticate_user!,only: [:Vote]
  respond_to :js,:json,:html

def Vote
    if !current_user.liked? @product
      @product.liked_by current_user
    elsif current_user.liked? @product
      @product.unliked_by current_user
    end
  end

路由文件

    resources :products do
    member do
      put "like" => "products#Vote"
    end
    # post "comments",to: "comments#create"
    resources :comments,only: [:create,:destroy]
  end

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)