我不断收到此错误,但在此处找不到解决此错误的答案。
我有一个 Book、User 和 Like 模型,如下所示:
class Book < ActiveRecord::Base
attr_accessible :title
has_many :likes
has_many :users, through: :likes
end
class User < ActiveRecord::Base
attr_accessible :name
has_many :likes
has_many :books, through: :likes
end
class Like < ActiveRecord::Base
attr_accessible :book, :user
belongs_to :book
belongs_to :user
end
对应的喜欢控制器:
# app/controllers/likes_controller.rb
class LikesController < ApplicationController
def index
# Assign the logged in user to @user
@user = current_user
# Grab all of the books and put them into an array in @books
@books = Book.all
end
def create
book = Book.find(params[:book_id])
Like.create(:book => book, :user => current_user)
redirect_to likes_path, :notice => "You just liked the book #{book.title}"
end
def destroy
like = Like.find(params[:id])
like.destroy
redirect_to likes_path, :notice => "You destroyed a like"
end
end
在我的 config/routers.rb 中:
MyApp::Application.routes.draw do
resources :likes
end
我有应该删除现有的链接,例如:
<% like = book.likes.where(:user_id => @user.id).first %>
<%= link_to "destroy like", likes_path(like.id), :method => :delete %
但是当我点击链接时,我得到了这个错误:
No route matches [DELETE] "/likes.7"