2012-01-19 71 views
-1

我在application_controller.rb處理異常嵌套資源與Mongoid ::錯誤:: DocumentNotFound軌3.1

class ApplicationController < ActionController::Base 
    protect_from_forgery 
    rescue_from Mongoid::Errors::DocumentNotFound, :with => :render_not_found 
    def render_not_found 
    render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false 
    end 
end 

然後我打電話

此代碼例如在我的路線做工精細.RB:

resources :posts 

的問題是,如果我有一個嵌套的資源像這樣的routes.rb:

resources :users do 
resources :posts 
end 

我有這樣的posts_controller.rb

class PostsController < ApplicationController 

end 
與此父

現在:用戶不起作用!我在每一個行動的寫從posts_controller.rb此嵌套的資源在未來的工作的罰款如。

def show 
@post = Post.find(params[:id]) 
respond_to do |format| 
    format.html # show.html.erb 
    format.json { render json: @post } 
end 
rescue 
render_not_found 
end 
+0

@JatinGanhotra我編輯了這個問題。它可以很好地運行'resources:users'的'resources:posts'輸出。如果我把'資源:用戶做 資源:帖子 結束'。不起作用。謝謝 – hyperrjas

+0

在這裏發佈'rake routes'的輸出。 –

回答

0

在這裏你的控制器代碼,

class Users::PostsController < ApplicationController 

end 

你有Users::Posts,但你沒有在上面的路線中指定PostsController的位置。

+0

謝謝,我怎麼能在routes.rb中指定PostsController的位置?,謝謝。 – hyperrjas

+0

對不起,但我已編輯的帖子,我檢查與':controller =>'users/posts'',但不起作用:( – hyperrjas

+0

試試這個: '資源:職位,:控制器=>「用戶/職位」' –