简体   繁体   English

Rails仅路由link_to仅生成get请求rails 5

[英]Rails Routes only link_to only generating get requests rails 5

I am running rails 5.0.2 and have been following along with https://gorails.com/episodes/liking-posts?autoplay=1 to add a like button on my products show page. 我正在运行Rails 5.0.2,并且一直在跟https://gorails.com/episodes/liking-posts?autoplay=1一起在我的产品显示页面上添加一个赞按钮。

I cannot for the life of me work out why I keep hitting the error 我无法为自己的生活弄清楚为什么我会不断出错

No route matches [GET] "/products/7199/like" 没有路线匹配[GET]“ / products / 7199 / like”

the repo for the tutorial is here https://github.com/gorails-screencasts/gorails-24-liking-posts/blob/master/app/views/posts/_likes.html.erb 该教程的仓库位于https://github.com/gorails-screencasts/gorails-24-liking-posts/blob/master/app/views/posts/_likes.html.erb

As per the tutorial, I have aa routes.rb like so, I added only [:create, :destroy] during testing to try to get the post request. 按照教程,我像这样有一个route.rb,我在测试期间仅添加了[:create,:destroy]以尝试获取发布请求。 I even tested without the member do. 我什至没有成员做测试。

  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

my likes.rb model is 我的likes.rb模型是

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

my likes_controller is nested in products. 我的likes_controller嵌套在产品中。 I added the @product.id as I am using friendly_id, and the tutorial uses the product id. 我在使用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

My Buttons are on my products show page( I did have them in a partial(_likes.html.erb) but moved them back for testing) 我的按钮在我的产品显示页面上(我确实将它们放在了部分(_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>

and a create.js.erb and a destroy.js.erb have been created in views/products/likes as follows 并在view / products / likes中创建了create.js.erb和destroy.js.erb,如下所示

create 创造

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

destroy 破坏

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

initially I thought I had a turbolinks issue( which I still may) but noticed that I am not hitting the Post, despite my code. 最初,我以为我遇到了turbolinks问题(我仍然可以),但是注意到尽管有我的代码,但我没有达到《邮政》。 here is the rake 这是耙子

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

my application.js file: 我的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();
});

and I have the following dependency files in my javascript folder 而且我的javascript文件夹中有以下依赖项文件

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

Any guidance you can give me to fix this would be appreciated. 您可以给我解决的任何指导,将不胜感激。

After a lot of pulling my hair out, I found that the original developer had added an application.coffee file that seemed to only handle the social share side of things. 经过大量的努力,我发现原始开发人员添加了一个application.coffee文件,该文件似乎只处理社交方面的事情。

I removed the offending file. 我删除了有问题的文件。 and all else worked. 其他所有工作。 Including the turbolinks issue. 包括turbolinks问题。 So essentially above is what you would need to add a like system to your products model. 因此,基本上以上就是您需要在产品模型中添加类似系统的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM