簡體   English   中英

Rails僅路由link_to僅生成get請求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”

該教程的倉庫位於https://github.com/gorails-screencasts/gorails-24-liking-posts/blob/master/app/views/posts/_likes.html.erb

按照教程,我像這樣有一個route.rb,我在測試期間僅添加了[:create,:destroy]以嘗試獲取發布請求。 我什至沒有成員做測試。

  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>

並在view / 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問題(我仍然可以),但是注意到盡管有我的代碼,但我沒有達到《郵政》。 這是耙子

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問題。 因此,基本上以上就是您需要在產品模型中添加類似系統的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM