0
簡單的問題。MVC中的好方法放置
將所有控制器相關的方法(但不能直接用於視圖)放在幫助器中是否是一種好的做法?
我的意思是,我的控制器僅包含new
,create
,edit
等方法。裏面這些方法通常有另一個叫類似的方法:
class SessionsController < ApplicationController
include SessionsHelper
...
def destroy
sign_out if signed_in?
redirect_to root_path
end
end
module SessionsHelper
def signed_in?
!current_user.nil?
end
def forget(user)
user.forget
cookies.delete(:user_id)
cookies.delete(:remember_token)
end
def sign_out
forget(current_user)
session.delete(:user_id)
@current_user = nil
end
end
最近我讀過的助手應該包括使用只考慮方法,所以我不太確定是否上述計數爲方法,並因此,我的方法是否是一種不好的做法。
有人可以指點我認爲這個好/壞做法的相關文件嗎?
有一件事。從關注的內部將'helper_method:signed_in?'放到ApplicationController中,並將它放在'include Concerns :: Session'之後。在內部調用'helper_method'會導致'undefined method'錯誤。我編輯了你的答案,但需要同行評審。 – Kappa
@Kappa:謝謝,添加了你的建議編輯。 –