新手问题 Rails API Only 模式下,怎么为 jbuilder 写 helper 方法?

wfwdex · 2016年09月29日 · 最后由 wfwdex 回复于 2016年09月29日 · 1903 次阅读

在完整的 Rails 下,可以为 views 写 helper 方法,在 API Only 模式下,除了在 lib 里写,还有别的更好的方法吗?

在 stackoverflow 上看到有人这么做

# app/controllers/application_controller.rb
class ApplicationController < ActionController::API
  include ActionController::ImplicitRender
  include ActionController::Helpers
end

# app/helpers/application_helper.rb
module ApplicationHelper
  def foo
    "bar"
  end
end

这样做真的好吗?

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