我刚刚进入Rails,并且我在理解某些语法元素时遇到了一些麻烦,我正在阅读的这些指南似乎理所当然。Rails - 重构控制器方法到应用程序控制器
今天下午我正在研究一个小样本项目,并试图重构一些代码。基本上,我想在我的导航栏中包含仅限管理员控件的链接,这些控件取决于当前正在查看的页面。这是我的原始代码:
class RoomsController < ApplicationController
before_filter :admin_controlls
def new
end
def create
end
def edit
end
def update
end
def index
end
def destroy
end
private
def admin_controlls
@adminControlls = "rooms"
end
end
我的想法是,这个代码本质上是相同的每个控制器,我想创建管理控制。唯一的区别是我想传递给View的标志变量@adminControlls的值。
于是,我尝试这样的:
class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper
def admin_controlls(page)
@adminControlls = page
end
end
,改变了房间控制器这样的:
class RoomsController < ApplicationController
before_filter admin_controlls "rooms"
def new
end
def create
end
def edit
end
def update
end
def index
end
def destroy
end
end
我觉得我的困惑与before_filter,来开始,为什么叫有一个方法需要一个符号。我不知道如何将字符串作为符号调用时将字符串传递给方法,并且我所有改变之前的过滤器以调用方法(如上所述)的努力都失败了。总的来说,我只是不觉得我正在掌握语法的一些微妙之处。如果任何人有一个会有所帮助的资源,我会很感激。
你看过'params [:controller]'吗? –