微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ActionController::UrlGenerationError in Products#index error inacts_as_votable

如何解决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 举报,一经查实,本站将立刻删除。