Rails 仅路由 link_to 仅生成获取请求 rails 5

Rails Routes only link_to only generating get requests rails 5

我是 运行 rails 5.0.2 并且一直在跟随 https://gorails.com/episodes/liking-posts?autoplay=1 在我的产品展示页面上添加一个赞按钮。

我这辈子都弄不明白为什么我总是遇到错误

没有路由匹配[GET]“/products/7199/like”

教程的 repo 在这里 https://github.com/gorails-screencasts/gorails-24-liking-posts/blob/master/app/views/posts/_likes.html.erb

根据教程,我有一个像这样的 routes.rb,我在测试期间只添加了 [:create, :destroy] 以尝试获得 post 请求。我什至在没有会员的情况下进行了测试。

  Rails.application.routes.draw do
  root to: "pages#home"

  devise_for    :users,
              :path => '',
              :path_names => {:sign_in => 'login', :sign_out => 'logout', :edit => 'profile'},
              :controllers => {:omniauth_callbacks => 'omniauth_callbacks',
                               :registrations => 'registrations'
              }

  resources :users, only: [:show]
  resources :vendors, only: [:show]
  resources :brands, only: [:show]
  resources :products do
    resource :like, only: [:create,:destroy], module: :products
    member do
      get :toggle_status
    end
  end

  resources :pages
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

end

我的 likes.rb 型号是

class Like < ActiveRecord::Base
  belongs_to :user
  belongs_to :product
end

我的 likes_controller 嵌套在产品中。我在使用 friendly_id 时添加了 @product.id,本教程使用产品 ID。

class Products::LikesController < ApplicationController
  respond_to :html, :js

  before_action :authenticate_user!
  before_action :set_product

  def create
    @product.likes.where(user_id: current_user.id).create!

    respond_to do |format|
      format.html { redirect_to @product.id}
      format.js
    end
  end

  def destroy
    @product.likes.where(user_id: current_user.id).destroy_all

    respond_to do |format|
      format.html { redirect_to @product.id }
      format.js
    end
  end

  private

  def set_product
    @product = Product.find(params[:product_id])
  end
end

我的按钮在我的产品展示页面上(我确实有它们的一部分(_likes.html.erb)但将它们移回进行测试)

<div id="product_<%= @product.id %>_likes" class="col-md-6">
        <% if user_signed_in? && current_user.likes?(@product) %>
            <%= link_to 'Unlike', product_like_path(@product.id), method: :delete, remote: true, class: 'btn btn-primary btn-lg btn-block' %>
        <%- else -%>
            <%= link_to 'Like', product_like_path(@product.id), method: :post, remote: true, class: 'btn btn-primary btn-lg btn-block' %>
        <% end %>
      </div>

并且在 views/products/likes 中创建了一个 create.js.erb 和一个 destroy.js.erb,如下所示

创建

$('#product_<%= @product.id %>_likes').html("<%=j render partial: 'products/likes', locals: {product: @product} %>");

摧毁

$('#product_<%= @product.id %>_likes').html("<%=j render partial: 'products/likes', locals: {product: @product} %>");

最初我以为我有一个 turbolinks 问题(我仍然可能)但注意到我没有点击 Post,尽管我的代码。这是耙子

ake routes | grep like
                product_like DELETE   /products/:product_id/like(.:format)  products/likes#destroy
                             POST     /products/:product_id/like(.:format)  products/likes#create

我的application.js文件:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file. JavaScript code in this file should be added after the last require_* statement.
// jquery_ujs allows us to use 'data-remote',
// 'data-type', and 'data-method' attributes
//
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//= require jquery
//= require bootstrap-sprockets
//= require jquery_ujs
//= require jquery-ui
//= require jquery.turbolinks
//= require html.sortable
//= require turbolinks
//= require turbolinks-compatibility
//= require_tree .

$.turbo.use('turbolinks:load', 'turbolinks:request-start');

var resetForms = function () {
    // this depends on your use
    // this is for foundation 6's abide
    $('form').each(function () {
        $(this).foundation('destroy');
    });
};

document.addEventListener("turbolinks:before-cache", function() {
    resetForms();
});

我的 javascript 文件夹中有以下依赖文件

application.coffee
cable.js
search.js
turbolinks-compatibility.coffee
search.coffee
loading.js
favorite_products.coffee

如果您能给我任何解决此问题的指导,我们将不胜感激。

费尽周折后,我发现原来的开发者添加了一个 application.coffee 文件,似乎只处理社交分享方面的事情。

我删除了有问题的文件。其他一切都奏效了。包括 turbolinks 问题。所以基本上上面是你需要向你的产品模型添加一个类似的系统。