我剛剛進入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]'嗎? –