开发者

Extend an action in a controller

I'm building a controller that sets the same variables in several actions. Something like this:

def one
  @a = 1
  @b=2
  @test = "One"
end

def two
  @a = 1
  @b = 2
  @test = "Two"
end

I'm aware that I could call a method to fill in the variable assignments, but I'm wondering how one would do this the "Best Practice" way. I got ambitious and tried...

def master 
  @a = 1
  @b = 2
end

def 开发者_如何学Pythonone < master
  @test = "One"
end

def two < master
  @test = "Two"
end

But this arose to no avail. What does the SO community suggest?


< is used for inheritance in Ruby and cannot be used on methods. In Rails you can call before_filter for this purpose.

before_filter :master

if you want it for all methods in the controller, or

before_filter :master, :only => [:one, :two]

if you want it for these methods only.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜