Rails 3.1 respond_to & render_403问题

发布于 2024-11-19 22:48:17 字数 1156 浏览 5 评论 0原文

我不确定它是什么。我刚刚从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

虫児飞 2024-11-26 22:48:17

好吧,想通了。可能不是 Rails 问题。至少只有在运行规范时才会出现问题。

我一直在检查 403 页面是否是这样渲染的:

response.should render_template("public/403.html")

不再起作用。更换它

response.status.should == 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:

response.should render_template("public/403.html")

Doesn't work no more. Replacing it with

response.status.should == 403

fixed the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文