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

wfwdex · September 29, 2016 · Last by wfwdex replied at September 29, 2016 · 1906 hits

在完整的 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

这样做真的好吗?

You need to Sign in before reply, if you don't have an account, please Sign up first.