Rails 3.1 respond_to & render_403问题
我不确定它是什么。我刚刚从 3.0.9 升级到 Rails 3.1,这是我在规格中得到的内容:
PeopleController edit action should require owner
Failure/Error: response.should render_template("/public/403.html")
expecting <"/public/403.html"> but rendering with <"search/_search_menu_item">
这是我在各种控制器中的规格。我的 AppController 中也有此代码:
def render_403
respond_to do |format|
format.html { render :file => "#{Rails.root}/public/403.html", :status => 403, :layout => false }
format.json { render :json => { :error => true, :message => "Error 403, you don't have permissions for this operation." } }
end
end
在 PeopleController 中也有此代码:
def edit
render_403 unless @person.account.id == current_account.id
end
我确信 format.html
块会被执行(检查过)。然而,规格预期落空。想知道这里发生了什么。
(search/_search_menu_item
是包含在每个页面上的部分内容,这基本上意味着应用布局会在此处呈现。)
更新: 我已替换 render_403 在
#edit
中使用 render(:file => "#{Rails.root}/public/403.html", :status => 403, :layout => false)
看看会发生什么 - 得到相同的结果。
I'm not sure what it is. I've just upgraded to Rails 3.1 from 3.0.9 and here's what I get in my specs:
PeopleController edit action should require owner
Failure/Error: response.should render_template("/public/403.html")
expecting <"/public/403.html"> but rendering with <"search/_search_menu_item">
This is all over my specs in various controllers. I also have this code in my AppController:
def render_403
respond_to do |format|
format.html { render :file => "#{Rails.root}/public/403.html", :status => 403, :layout => false }
format.json { render :json => { :error => true, :message => "Error 403, you don't have permissions for this operation." } }
end
end
And this in PeopleController:
def edit
render_403 unless @person.account.id == current_account.id
end
I'm certain that format.html
block gets executed (checked it). However the spec expectation fails. Wonder what is going on here.
(search/_search_menu_item
is a partial that gets included onto every page, which basically means that the app layout gets rendered here instead.)
Update: I've replaced render_403
in #edit
with render(:file => "#{Rails.root}/public/403.html", :status => 403, :layout => false)
to see what happens - got the same result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,想通了。可能不是 Rails 问题。至少只有在运行规范时才会出现问题。
我一直在检查 403 页面是否是这样渲染的:
不再起作用。更换它
解决了问题。
Ok, figured it out. Possibly not a Rails problem. At least the problem appears only when running the specs.
I've been checking if 403 pages are rendered with this:
Doesn't work no more. Replacing it with
fixed the issue.