2013-09-05 16 views
5

* alt * güncellenirMongoid :: Hatalar :: DocumentNotFound raise_not_found_error

, ben alıyorum var doesnt o kullanıcı arayan:

Mongoid :: Hatalar

:: DocumentNotFound UsersController içinde #show

sorun: kimlikleri bir kimliği ya da dizi ile User.find çağrı, her bir parametre gerekir ma: id (ler) 22. Özet sınıfı kullanıcı için bulunmayan Belgesi (lar) Veritabanındaki bir dokümana tökez veya bu hata ortaya çıkar. araması id (s): 22 ... (1 toplam) içindi ve şu kimlikler bulunamadı: 22. Çözünürlük: Veritabanındaki bir kimliği arayın veya Mongoid.raise_not_found_error yapılandırma seçeneğini olarak ayarlayın false için, tek bir kimlik için arama yaparken bu hatayı yükseltmek yerine döndürülmesine neden olur, ya da sadece katları ararken eşleştirilmiş belgeler.

Ancak ben değil (yanlış

mongoid.yml için

development: 
    adapter: 'mongoid' 
    # 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: blog_development 
     # Provides the hosts the default session can connect to. Must be an array 
     # of host:port pairs. (required) 
     hosts: 
     - localhost:27017 
     options: 
      allow_dynamic_fields: false 
      identity_map_enabled: true 
      include_root_in_json: true 
      include_type_for_serialization: true 
      # Note this can also be true if you want to preload everything, but this is 
      # almost never necessary. Most of the time set this to false. 
      preload_models: 
      - Canvas 
      - Browser 
      - Firefox 
      scope_overwrite_exception: true 
      raise_not_found_error: false 
      skip_version_check: false 
      use_activesupport_time_zone: false 
      use_utc: true 
    # Configure Mongoid specific options. (optional) 
    options: 
    # 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: false 

    # 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 

    # Use 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 
test: 
    sessions: 
    default: 
     database: blog_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 

Kontrolör

# GET /users/1 
    # GET /users/1.json 
    def show 

    @user = User.find(params[:id]) 

    render json: @user 
    end 

* GÜNCELLEME ** sabit boş yanıtı raise_not_found_error ayarlıyorum json formatı) bunu yaparak çıkıyor:

def show 

    @user = User.find(params[:id]) 
    if @user.nil? 
     @user = [] 
    end 

    render json: @user 
    end 
+0

Cevabımı düzenledim, bu çözümün olması için iyi bir şans var. Umarım =) – okliv

+0

Ayrıca yapabilirsiniz: @user = User.find (params [: id]) || [] 'doğrudan – tpei

cevap

13

YML sizin yapısı

olmak zorunda yanlıştır -

development: 
    sessions: 
    options: 
     #raise_not_found_error has to be not here but see below 
    options: #strictly 2 spaces before 
    raise_not_found_error: false #strictly 4 spaces before not 6 

yüzden, raise_not_found_error parametre beni bile doğru girintili için development>options çocuğu değil development>sessions>options

+0

Teşekkürler bu bana yapılandırma düzeyinde olmamız gereken yere olsun yardımcı oldu. Ancak şimdi geri dönen ve JSON formatında değil. – azz0r

+0

nasıl hakkında 'response_to {| format | format.json {render: json => @user}} '? – okliv

+0

verir: ActionController :: UnknownFormat in UsersController # show Ben rails-api – azz0r

5

olmak zorunda işe yaramadı, config/initializers içinde mongoid.rb adında bir başlatıcı dosyası oluşturmak için ne yaptı ve bunu içine koydu

Mongoid.raise_not_found_error = false