2017-05-23 6 views
0

rails 5.0.2を実行していて、https://gorails.com/episodes/liking-posts?autoplay=1と一緒に私の製品のshowページに好きなボタンを追加しています。Railsルートのみlink_to getリクエストを生成するrails 5

私はエラー

Noルートの一致を打っておく理由は、私は私の人生のために働くことができない

チュートリアルのレポはこちらhttps://github.com/gorails-screencasts/gorails-24-liking-posts/blob/master/app/views/posts/_likes.html.erb

である「7199 /のように/ /製品」[GET]を

チュートリアルの通り、私はroutes.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モデルは私のlikes_controllerが製品にネストされている

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

です。 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 

私のボタンが私の製品にある

<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> 

とcreate.js(私は部分(_likes.html.erbでそれらを持っていた)が、テストのためにそれらを戻す)ページを表示します。

が破壊

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

を作成し、以下のようにERBとdestroy.js.erbは、ビュー/製品で作成された/好き

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

は当初、私はturbolinksの問題(私はまだかもしれないが)思っていたが、私は自分のコードにもかかわらず、ポストに当たっていないことに気づいています。

// 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 

あなたが修正するために私を与えることができます任意の指導を持っている:ここでの熊手は

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ファイルでありますこれは高く評価されます。

+0

application.jsファイルにjquery-ujsがありますか?https://github.com/gorails-screencasts/gorails-24-liking-posts/blob/master/app/assets/javascripts/application .js? –

+0

はい、 'application.js'に' jquery_ujs'が必要です – JCorcuera

+0

こんにちは@RyanDrost、私はファイルを表示するように更新しました –

答えて

0

私の髪を引っ張ってしまった後、元の開発者は物事のソーシャルシェアサイドを処理するように見えるapplication.coffeeファイルを追加したことがわかりました。

問題のファイルを削除しました。他のすべては働いた。ターボの問題を含む。したがって、本質的に上記のようなシステムを製品モデルに追加する必要があります。

関連する問題