【发布时间】:2012-01-19 15:13:21
【问题描述】:
我的 application_controller.rb
中有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
然后我打电话
此代码在我的 routes.rb 中运行良好:
resources :posts
问题是如果我在 routes.rb 中有这样的嵌套资源:
resources :users do
resources :posts
end
我在 posts_controller.rb
中有这个class PostsController < ApplicationController
end
现在这个父 :users 不起作用!。我在 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
【问题讨论】:
-
@JatinGanhotra 我已经编辑了这个问题。它确实适用于来自
resources :users的resources :posts输出。如果我输入resources :users do resources :posts end。不工作。谢谢 -
在这里发布
rake routes的输出。
标签: ruby-on-rails ruby-on-rails-3 exception-handling mongoid