2016-11-23 226 views
0

我有一個rails應用程序,我正在遵循並取消關注用戶權限不是我有創建方法工作,但不是刪除方法,我不知道我爲什麼得到這個錯誤。沒有路由匹配[DELETE]「/跟着」rails

class User < ActiveRecord::Base 
 
    # Include default devise modules. Others available are: 
 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
 
    devise :database_authenticatable, :registerable, 
 
     :recoverable, :rememberable, :trackable, :validatable 
 
      
 
    has_many :post 
 
    has_many :likes 
 
    has_many :follows 
 
    acts_as_commontator 
 
end 
 

 
           
 
           
 
class Follow < ActiveRecord::Base 
 
    belongs_to :user 
 
end

​​

class FollowsController < ApplicationController 
 
    def create 
 
    follow = Follow.new 
 
    follow.user_id = current_user.id 
 
    follow.follows = params[:user_id] 
 
    follow.save 
 
    redirect_to root_path 
 
    end 
 

 
    def destroy 
 
    follow = Follow.where('user_id = ? AND follows = ?',current_user.id,params[:user_id]) 
 
    Follow.destroy 
 
    redirect_to root_path 
 
    end 
 
    def index 
 
    @users = User.all 
 
    end 
 
end

我不知道爲什麼該方法不WO rking。

user_id字段是當前登錄用戶的字段,下面的字段是用戶遵循的用戶字段。

+0

您可以發佈您的路由文件中的相關章節? – mysmallidea

+0

devise_for:用戶 資源:喜歡 刪除 '遵循/:USER_ID'=> '如下#摧毀' 資源:帖子 資源:如下 安裝Commontator ::引擎=> '/ commontator' 根 'news_feed#指數' –

回答

0

您缺少將http方法(delete)和url(從following_path和user_id生成)映射到相關控制器操作的路由。

在你的routes.rb文件中添加以下內容:

delete 'follows/:user_id' => 'follows#destroy' 
+0

我有資源:在我的路線發帖雖然 –

+0

的帖子?好吧,我很困惑......我以爲你在試圖取消關注時在FollowsController中出現錯誤。這些帖子到哪裏呢? – David

+0

對不起,我的意思是資源: –

0

變化follows_path奇異訪問DELETE路徑爲資源:

<%= link_to 'Unfollow', follow_path({user_id: user.id}), method: :delete %> 
+0

沒有路由匹配{:action =>「show」,:controller =>「following」,:user_id => 1}缺少必需的鍵:[:id] –

相關問題