没有路线匹配[DELETE]“/ follow”rails

时间:2016-11-23 21:58:25

标签: ruby-on-rails

我有一个rails应用程序,我正在跟随并取消关注用户权限,我没有使用create方法但是没有delete方法,我不知道为什么我收到此错误。

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

<% @users.try(:each) do |user| %>
        <tr>
          <td><%= user.email %></td>
          <td>
            <% if follow = user.follows.find_by(follows: user.id) %>
                <%= link_to 'Unfollow', follows_path({user_id: user.id}), method: :delete%>
                <%= link_to 'Unfollow', follows_path({user_id: user.id}), method: 'delete',class: "btn btn-primary"  %>
            <% else %>
                <%= link_to 'Follow', follows_path({user_id: user.id}), method: 'post',class: "btn btn-primary"  %>
            <% end %>
          </td>
        </tr>
    <% 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

我不知道为什么这个方法不起作用。

user_id字段是当前登录用户的字段,以及用户所关注的用户的以下字段。

2 个答案:

答案 0 :(得分:0)

您缺少将http方法(删除)和url(从following_path和user_id生成)映射到相关控制器操作的路由。

在routes.rb文件中添加以下内容:

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

答案 1 :(得分:0)

follows_path更改为单数以访问该资源的DELETE路径:

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