2014-02-07 30 views
0

一個高效的代碼我有,我想在一個單獨的行動來寫這些變量並調用動作的名稱,我已經使用變量,路線寫在控制器

def act1 
@a=... 
@b=.... 
@c=... 
end 

def act1 
@a=... 
@b=.... 
@c=... 
end 

def act2_ajax 
@a=... 
@b=.... 
@c=... 
end 

def act3 
@a=... 
@b=.... 
@c=... 
end 

def act4_ajax 
@a=... 
@b=.... 
@c=... 
end 

不同的頁面中的動作控制器@一,@b @c的意見,如果我叫一個單獨的行動,這些變量應該來

爲前可以說我們寫

def common(xx) 
    @a=... 
    @b=... 
    @c=... 
end 

我們內部ACT1俗物一樣

def act1 
    self.common1(var) 
end 

其輸出對子級像

def act1 
    @a=... 
    @b=... 
    @c=... 
end 

回答

1
before_action :set_vars, only: [:act1, :act2, :act3] 

private 
    def set_vars 
    @a = ... 
    @b = ... 
    @c = ... 
    end 

而且在你的行動沒有額外線!

+0

這個問題被標記爲'ruby-on-rails-3'並且'before_action'已經在Rails 4.0.0中引入,所以你的意思可能是'before_filter' – trushkevich