新手问题 请问,著名的 RailsTutorail 教程的这段代码是不是有问题?

wcc526 · 2014年05月19日 · 最后由 shihangw 回复于 2014年05月20日 · 2080 次阅读

请问,著名的 RailsTutorail 教程的这段代码是不是有问题?

http://railstutorial-china.org/chapter8.html

代码 8.14:在 Application 控制器中引入 Sessions 控制器的帮助方法模块

app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  include SessionsHelper
end

Helper 使用场景,应该是用在抽取 View 的复杂方法。 我觉得 RailsTutorial 不应该把这个 Helper 方法抽取之后,又直接在 ApplicationController 里面引入。

我看@Rei的 campo 都是直接在 application_controller 里面写的。 https://github.com/chloerei/campo/blob/master/app/controllers/application_controller.rb ruby-china 也是这样做的。 https://github.com/huacnlee/ruby-china/blob/master/app/controllers/application_controller.rb

感觉这样在 application_controller 使用 include 不太好,还不如直接写到 application_controller 里面去?

大家的意见如何?谢谢!

差不多,这样省的定义 helper_method 了。

上面那两个可能都是 Rei 写的,所以风格一样。

session 相关的一部分代码放在 helper 里面是恰当的,比如获取当前用户名。我觉得最好是把这堆代码放 lib 里,然后在 controllers 和 helpers 里调用它们。Refactor 的终点是你会把它们全部抽到一个 gem 里,这个 gem 就叫做 devise

需要 登录 后方可回复, 如果你还没有账号请 注册新账号