使用嵌套资源的基础知识有问题。
我有用户(基于设计),有很多Emailtasks。但是,在尝试销毁电子邮件任务时,我收到“未找到文档错误”。确实有一个具有此用户ID的用户手动检查数据库,该用户具有此id的电子邮件任务。
路线:
authenticated :user do
root :to => 'users#index'
resources :emailtasks
end
devise_scope :user do
root :to => "devise/registrations#new"
match '/user/confirmation' => 'confirmations#update', :via => :put, :as => :update_user_confirmation
resources :emailtasks
end
devise_for :users, :controllers => { :registrations => "registrations", :confirmations => "confirmations" }
resources :users do
get 'invite', :on => :member
resources :emailtasks
end
(我在每种类型的用户下面嵌套了“emailtasks” - 如果这有问题,请继续学习)
Emailtasks控制器。索引工作正常,销毁不行。
def index
@user = current_user
@emailtasks = @user.emailtasks.all
end
def destroy
@user = current_user
emailtask = Emailtask.find(params[:id])
@user.emailtask.destroy
respond_to do |format|
format.html { redirect_to emailtask_url }
format.json { head :no_content }
end
end
索引视图:
- @emailtasks.each do |emailtask|
%tr
%td= link_to("Delete emailtask", emailtask_path(emailtask), :data => { :confirm => "Are you sure?" }, :method => :delete, :class => 'btn btn-mini')
单击destroy时,这是错误:
Started DELETE "/users/50b44012d4aadb1ea4000005/emailtasks/50b55057d4aadbaeaf000003" for 127.0.0.1 at 2012-12-01 13:14:45 -0500
Processing by EmailtasksController#destroy as HTML
Parameters: {"authenticity_token"=>"znRrduerykwNAh9MrBT/oRZgtSuPzznSsmuGgZm7EDE=", "user_id"=>"50b44012d4aadb1ea4000005", "id"=>"50b55057d4aadbaeaf000003"}
MOPED: 127.0.0.1:27017 COMMAND database=admin command={:ismaster=>1} (0.8771ms)
MOPED: 127.0.0.1:27017 QUERY database=addtocal_development collection=users selector={"$query"=>{"_id"=>"50b44012d4aadb1ea4000005"}, "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=-1 skip=0 fields=nil (0.8090ms)
MOPED: 127.0.0.1:27017 QUERY database=addtocal_development collection=emailtasks selector={"_id"=>"50b55057d4aadbaeaf000003"} flags=[:slave_ok] limit=0 skip=0 fields=nil (0.3309ms)
Completed 500 Internal Server Error in 6ms
Mongoid::Errors::DocumentNotFound (
Problem:
Document(s) not found for class Emailtask with id(s) 50b55057d4aadbaeaf000003.
Summary:
When calling Emailtask.find with an id or array of ids, each parameter must match a document in the database or this error will be raised. The search was for the id(s): 50b55057d4aadbaeaf000003 ... (1 total) and the following ids were not found: 50b55057d4aadbaeaf000003.
Resolution:
Search for an id that is in the database or set the Mongoid.raise_not_found_error configuration option to false, which will cause a nil to be returned instead of raising this error when searching for a single id, or only the matched documents when searching for multiples.):
用户模型:
class User
include Mongoid::Document
extend Rolify
rolify
include Mongoid::Timestamps
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :invitable, :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
## Database authenticatable
field :email, :type => String, :default => ""
field :encrypted_password, :type => String, :default => ""
embeds_many :emailtasks
accepts_nested_attributes_for :emailtasks
validates_presence_of :email
...
电子邮件任务模型:
class Emailtask
include Mongoid::Document
include Mongoid::Timestamps # adds automagic fields created_at, updated_at
...
embedded_in :user
思考?谢谢!
更新:这是当地mongodb的记录:
{ "_id" : ObjectId( "50b44012d4aadb1ea4000005" ),
"confirmed_at" : Date( 1353990162412 ),
"created_at" : Date( 1353990162412 ),
"current_sign_in_at" : Date( 1354143479625 ),
"current_sign_in_ip" : "127.0.0.1",
"email" : "theiteg@gmail.com",
"emailtasks" : [
{ "_id" : ObjectId( "50b522f2d4aadbaeaf000001" ),
"content" : null,
"name" : null,
"actuallyfrom" : null,
"updated_at" : Date( 1354048241990 ),
"created_at" : Date( 1354048241990 ) },
{ "_id" : ObjectId( "50b54fc9d4aadbaeaf000002" ),
"content" : null,
"name" : null,
"actuallyfrom" : null,
"updated_at" : Date( 1354059721425 ),
"created_at" : Date( 1354059721425 ) },
{ "_id" : ObjectId( "50b55057d4aadbaeaf000003" ),
"content" : null,
"name" : null,
"actuallyfrom" : null,
"updated_at" : Date( 1354059863314 ),
"created_at" : Date( 1354059863314 ) },
此外 - 手动编辑此本地数据库(更改值,添加/销毁记录等)可在通过localhost rails应用程序查看时创建预期的更改。
完整的mongoid.yml:
production:
sessions:
default:
uri: <%= ENV['MONGOHQ_URL'] %>
development:
# Configure available database sessions. (required)
sessions:
# Defines the default session. (required)
default:
# Defines the name of the default database that Mongoid can connect to.
# (required).
database: addtocal_development
# Provides the hosts the default session can connect to. Must be an array
# of host:port pairs. (required)
hosts:
- localhost:27017
options:
# Change whether the session persists in safe mode by default.
# (default: false)
# safe: false
# Change the default consistency model to :eventual or :strong.
# :eventual will send reads to secondaries, :strong sends everything
# to master. (default: :eventual)
# consistency: :eventual
# How many times Moped should attempt to retry an operation after
# failure. (default: 30)
# max_retries: 30
# The time in seconds that Moped should wait before retrying an
# operation on failure. (default: 1)
# retry_interval: 1
# Configure Mongoid specific options. (optional)
options:
# Configuration for whether or not to allow access to fields that do
# not have a field definition on the model. (default: true)
# allow_dynamic_fields: true
# Enable the identity map, needed for eager loading. (default: false)
# identity_map_enabled: false
# Includes the root model name in json serialization. (default: false)
# include_root_in_json: false
# Include the _type field in serializaion. (default: false)
# include_type_for_serialization: false
# Preload all models in development, needed when models use
# inheritance. (default: false)
# preload_models: false
# Protect id and type from mass assignment. (default: true)
# protect_sensitive_fields: true
# Raise an error when performing a #find and the document is not found.
# (default: true)
# raise_not_found_error: true
# Raise an error when defining a scope with the same name as an
# existing method. (default: false)
# scope_overwrite_exception: false
# Skip the database version check, used when connecting to a db without
# admin access. (default: false)
# skip_version_check: false
# User Active Support's time zone in conversions. (default: true)
# use_activesupport_time_zone: true
# Ensure all times are UTC in the app side. (default: false)
# use_utc: false
production:
sessions:
default:
database: addtocal
username: <%= ENV['MONGO_UN'] %>
password: <%= ENV['MONGO_PW'] %>
hosts:
- alex.mongohq.com:10071
options:
skip_version_check: true
safe: true
test:
sessions:
default:
database: addtocal_test
hosts:
- localhost:27017
options:
consistency: :strong
# In the test environment we lower the retries and retry interval to
# low amounts for fast failures.
max_retries: 1
retry_interval: 0