开发者

Ruby on Rails: how do I call an action that has params from another action

开发者 https://www.devze.com 2023-01-07 06:57 出处:网络
So I want to do this: save_to_library(params) if params[:commit] == \"lib\" but save_to_library apparently doesn\'t take any arguments.

So I want to do this:

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

but save_to_library apparently doesn't take any arguments.

h开发者_C百科ow 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)


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消