【发布时间】:2014-08-07 16:45:58
【问题描述】:
我在我的 Rails 应用程序中使用 mongoig_slug gem。
我可以使用正确的 url 创建一个对象,但我无法更新/删除该对象,并且控制台中没有错误消息。 (我可以编辑包含正确数据的表单)
我什至使用 PRY 来检查控制台,当我检查 @book 退出并且是否正确时,如果我执行 @book.destroy,它会显示为 true,但不会破坏。 对于编辑,我还检查了@book,我还检查了正确的 book_params。
class Book
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Slug
field :_id, type: String, slug_id_strategy: lambda {|id| id.start_with?('....')}
field :name, type: String
slug :name
end
class BooksController < ApplicationController
before_filter :get_book, only: [:show, :edit, :update, :destroy]
def update
if @book.update_attributes(book_params)
redirect_to book_path(@book)
else
flash.now[:error] = "The profile was not saved, please try again."
render :edit
end
end
def destroy
binding.pry
@book.destroy
redirect_to :back
end
def book_params
params.require(:book).permit(:name)
end
def get_book
@book = Book.find params[:id]
end
end
【问题讨论】:
-
是
@startup.destroy,不是@book.destroy,所以你不要毁了这本书。 -
这只是一个错字,因为我在 2 个不同的项目中,但我的代码是 @book.destroy
-
我更新了答案,希望对您有所帮助。 This example 为我正常工作。
标签: ruby-on-rails-4 mongoid slug