Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails &引用;“销毁”;操作未找到查询的资源_Ruby On Rails_Mongodb_Mongoid_Nested Attributes - Fatal编程技术网

Ruby on rails &引用;“销毁”;操作未找到查询的资源

Ruby on rails &引用;“销毁”;操作未找到查询的资源,ruby-on-rails,mongodb,mongoid,nested-attributes,Ruby On Rails,Mongodb,Mongoid,Nested Attributes,在使用嵌套资源的基础知识方面遇到问题 我有一些用户(基于Desive),他们有许多电子邮件任务。但是,在尝试销毁emailtask时,我收到一个“document not found error”(文档未找到错误)。手动检查数据库时,确实有一个用户具有此用户id,他拥有一个具有此id的emailtask 路线: authenticated :user do root :to => 'users#index' resources :emailtasks

在使用嵌套资源的基础知识方面遇到问题

我有一些用户(基于Desive),他们有许多电子邮件任务。但是,在尝试销毁emailtask时,我收到一个“document not found error”(文档未找到错误)。手动检查数据库时,确实有一个用户具有此用户id,他拥有一个具有此id的emailtask

路线:

    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”-如果这有问题,欢迎学习)

电子邮件任务控制器。索引工作正常,销毁则不行

  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')
单击销毁后,出现以下错误:

    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
生产:
会议:
违约:
uri:
发展:
#配置可用的数据库会话。(必选)
会议:
#定义默认会话。(必选)
违约:
#定义Mongoid可以连接到的默认数据库的名称。
#(必需)。
数据库:addtocal_开发
#提供默认会话可以连接到的主机。必须是数组
#主机类型:端口对。(必选)
主持人:
-本地主机:27017
选项:
#更改会话是否默认保持在安全模式。
#(默认值:false)
#安全:错误
#将默认一致性模型更改为:final或:strong。
#:final将向二级数据库发送读取,:strong将发送所有内容
#掌握。(默认:最终)
#一致性:最终的
#之后,Moped应尝试重试操作多少次
#失败。(默认值:30)
#最多重试次数:30次
#轻便摩托车在重试前应等待的时间(以秒为单位)
#故障时的操作。(默认值:1)
#重试间隔:1
#配置特定于Mongoid的选项。(可选)
选项:
#是否允许访问不允许访问的字段的配置
#模型上没有字段定义。(默认值:true)
#允许\u动态\u字段:true
#启用即时加载所需的标识映射。(默认值:false)
#已启用标识映射:false
#在json序列化中包含根模型名称。(默认值:false)
#将_root_包含在_json中:false
#在序列化中包含_type字段。(默认值:false)
#包含\u序列化的\u类型\u:false
#预加载开发中的所有模型(模型使用时需要)
#继承权。(默认值:false)
#预加载\u模型:错误
#保护id和类型不受质量分配的影响。(默认值:true)
#保护敏感字段:true
#执行#查找但未找到文档时引发错误。
#(默认值:true)
#raise\u not\u found\u错误:true
#定义与对象同名的作用域时引发错误
#现有方法。(默认值:false)
#范围覆盖异常:false
#跳过数据库版本检查,该检查在连接到数据库时使用
#管理员权限。(默认值:false)
#跳过版本检查:false
#用户活动支持的时区转换。(默认值:true)
#使用\u activesupport\u时区:true
#确保应用程序端的所有时间均为UTC。(默认值:false)
#使用utc:false
制作:
会议:
违约:
数据库:addtocal
用户名:
密码:
主持人:
-alex.mongohq.com:10071
选项:
跳过版本检查:真
安全:对
测试:
会议:
违约:
数据库:addtocal_测试
主持人:
-本地主机:27017
选项:
一致性:强
#在测试环境中,我们将重试次数和重试间隔降低到
#快速故障的低金额。
最大重试次数:1次
重试间隔:0

我不知道这是否有帮助,但您正在根据任务id查找任务,并尝试销毁@user.emailtasks(这部分永远无法访问,它无论如何都不应该工作,因为销毁集合的正确方法是调用destroy\u all)。不管怎样,您是否检查过您的应用程序是否已在正确的环境中加载?这已经发生在我身上,我花了几个小时才明白我没有连接到正确的数据库。。。(在生产模式下启动的应用程序…duh)我确实检查了环境,它加载正确/未在生产模式下启动-如果这是问题的话,那就太好了!你的第一点似乎是我做错了什么,但我尝试的几次迭代都没有成功,所以我想问问专家。不确定我设置的哪些基础(模型、关联、控制器方法及其使用的参数、视图)不正确。可能在“索引”视图中,它应该是要销毁的链接中的用户\电子邮件任务\路径?但仅进行此更改就会在重新加载“索引”时导致路由错误。(“没有路由匹配{:action=>“show”,:controller=>“emailtasks",:user_id=>#问题是,如果您确定数据库中存在id为50B5557D4AADBEAF000003的记录,这不是rails相关的问题,或者至少是配置问题。您将好的id传递给操作,此时您所要做的就是找到它。因此,如果它在您的数据库中,而mongoid没有找到它,那么问题就来了从mongoid…再次检查记录是否存在,如果不存在,请检查您的配置(您的database.yml看起来像什么?)是否合理