【发布时间】:2011-07-12 23:22:05
【问题描述】:
是否可以在 Rails 3 的 before_filter 方法中重置默认布局?
我的 contacts_controller.rb 有以下内容:
class ContactsController < ApplicationController
before_filter :admin_required, :only => [:index, :show]
def show
@contact = Contact.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @contact }
end
end
[...]
end
以及我的 application_controller.rb
中的以下内容class ApplicationController < ActionController::Base
layout 'usual_layout'
private
def admin_required
if !authorized? # please, ignore it. this is not important
redirect_to[...]
return false
else
layout 'admin' [???] # this is where I would like to define a new layout
return true
end
end
end
我知道我可以放...
layout 'admin', :only => [:index, :show]
...在“ContactsController”中的“before_filter”之后,但是,由于我已经有一堆其他控制器,其中许多操作被正确过滤为管理员所需的操作,如果我可以重置会容易得多在“admin_required”方法中从“usual_layout”到“admin”的布局。
顺便说一句,通过放...
layout 'admin'
...在“admin_required”内(正如我在上面的代码中所尝试的那样),我收到一条未定义的方法错误消息。它似乎只在 defs 之外工作,就像我为“usual_layout”所做的那样。
提前致谢。
【问题讨论】:
标签: ruby-on-rails layout before-filter