views:

20

answers:

2

So I want to do this:

save_to_library(params) if params[:commit] == "lib"

but save_to_library apparently doesn't take any arguments.

how do actions get params if they don't take arguments?

(I know this action works when I link to it directly... just trying to streamline usability)

+2  A: 

Your controller processes the params and makes them available to you through an accesor method, they are available to your whole controller without the need to pass it around in method parameters.

Francisco Soto
+1  A: 

params is a global hash, imagine it as if it were defined outside the method:

params = {:commit => "lib"}

def save_to_library
  @var = params[:commit]
  # etc..
end

If you want to do some conditional actions you can just do this:

def update
  save_to_library if params[:commit] == "lib"
end

def save_to_library
  @var = params[:commit]   # @var = "lib"
  # etc..
end

And it should just work.

Karl
Is not really a global variable, it would otherwise have a $ at the beginning of its name, its an instance variable wrapped in an accessor method, that is why you can access it without the @.
Francisco Soto
Yeah, I should've clarified there; it is in the scope of the controller so all actions can access it without passing it around.
Karl