I’m just getting into Rails and I’m having some trouble understanding some syntax elements that the guides I’m reading just seem to take for granted.
This afternoon I was working on a small sample project and was trying to refactor some code. Basically, I want to include links to admin only controls in my nav bar that are dependent on which page is currently being viewed. This was my original code:
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
My thinking was that this code would essentially be the same in every controller where I want to create admin controls. The only difference being the value of the flag variable @adminControlls I want to pass to the View.
So, I attempted this:
class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper
def admin_controlls(page)
@adminControlls = page
end
end
And changed the Rooms controller to this:
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
I think my confusion starts with the before_filter and why calling a method there requires a symbol. I can’t figure out how to pass a string into the method when it is called as a symbol, and all my efforts to change the before filter to call a method (as above) have failed. Overall I’m just not feeling like I’m grasping some subtleties of the syntax. If anyone has a resource that would be helpful, I’d appreciate it.
Could you try this:
before_filter { |c| c.admin_controlls "rooms" }? A similar question could be found here: How can I send a parameter to a before filter?